I don't know where I went wrong here, I'm trying to make a random choice attack system, well kind of but instead it's not waiting and at all for anything but probably for the first wait time. The results I'm looking for is: wait for amount of seconds then pick a random attack then change color and go back to original color then repeat process
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossMovement : MonoBehaviour
{
// changes color
public Color originalColor;
public Transform spawnPoint;
// weapons
//choses a Attackmove to do
int AttackChoice;
public float distanceStop;
public float distanceR;
/// changes the material of the boss
private Material mymaterial;
//animator
public Animator anim;
public float distance;
public float distance3;
// player
public Transform target;
// how fast we are going
public float speed = 4f;
public float speedR;
Rigidbody rig;
// Start is called before the first frame update
private void Start()
{
rig = GetComponent<Rigidbody>();
anim = GetComponentInChildren<Animator>();
}
private void Update()
{
whento();
StopFollowing();
StartCoroutine(AttackChoices());
}
// Update is called once per frame
void followPlayer()
{
var step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(rig.transform.position, target.position, step);
}
IEnumerator AttackChoices()
{
yield return new WaitForSeconds(15);
AttackChoice = Random.Range(0, 4);
if (AttackChoice == 1)
{
gameObject.GetComponentInChildren<Renderer>().material.color = Color.white;
yield return new WaitForSeconds(15);
gameObject.GetComponentInChildren<Renderer>().material.color = originalColor;
}
if (AttackChoice == 2)
{
gameObject.GetComponentInChildren<Renderer>().material.color = Color.blue;
yield return new WaitForSeconds(15);
gameObject.GetComponentInChildren<Renderer>().material.color = originalColor;
}
if (AttackChoice == 3)
{
gameObject.GetComponentInChildren<Renderer>().material.color = Color.red;
yield return new WaitForSeconds(15);
gameObject.GetComponentInChildren<Renderer>().material.color = originalColor;
}
if (AttackChoice == 4)
{
yield return new WaitForSeconds(15);
}
}
void whento()
{
// follow player
transform.LookAt(target);
//raycast that shoots at player
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, distance))
{
if (hit.transform.CompareTag("Player"))
{
followPlayer();
anim.SetFloat("Speed", 1, 0f, Time.deltaTime);
}
}
else
{
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
}
void StopFollowing()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, distance3))
{
if (hit.transform.CompareTag("Player"))
{
distance = distanceStop;
}
}
else
{
distance = distanceR;
}
}
private void ChargeUpAndRealse()
{
anim.SetLayerWeight(anim.GetLayerIndex("ChargeLayer"), 1);
anim.SetTrigger("Charge");
anim.SetLayerWeight(anim.GetLayerIndex("ChargeLayer"), 0);
}
}