Waiting for time for enemy to attack
I just want to wait the 41.5 seconds for the enemies to attack. Here's my code. Any help would be awesome. Thanks!
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("player").transform; //target the player
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
yield WaitForSeconds(41.5);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
And sorry for not elaborating. The enemies just stand there endlessly.
Answer by HenryStrattonFW · Jan 28, 2017 at 11:41 AM
I'm not sure about UnityScript as I',m a C# programmer. However I believe your yield still needs to be a yield return
yield return new WaitForSeconds(41.5f);
Additionally unsure if you can turn the Update method into a coroutine and still have unity treat it as the normal update method or not, in C# we get an error logged out saying "Update() can not be a coroutine."
I'd try to offer a suggestion as to how to re-write your logic but I'm unsure what you want it to do, do you want update to run again after the 41.5 second delay (essentially giving your script just a really slow update rate)? If you can desribe the desired behaviour I might be able to suggest a solution, assuming of course that the change to the yield instruction is not enough to fix it for you.