- Home /
CoRoutione from FixedUpdate not working
Hello everyone, I am currently working on an underwater game, where a shark patrols, and once you hit his ray, he will target, then charge you, then turn to face you again.
The problem is, he keeps doing the charging and turning rapid fire. On startup, he will charge and turn like normal, but he keeps getting faster and faster going between the two attacks. I've tried Yield WaitforSeconds, Yield WaitforFixedUpdate, Co-routine, and tried a few Booleans, but it doesn't seem to be having any effect.
Would someone be kind enough to look at my script and help point me in the right direction?
Thank you very much.
var animalSpeed: float;
var rotSpeed : float = 10;
var moveDir : int = 1;
var provoked : boolean = false;
var attacking : boolean = false;
var turning : boolean = false;
var sharkAnimator : Animator;
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var loseSight = 120;
var MinDist = 5;
function Start ()
{
sharkAnimator.SetBool("hurt", false);
sharkAnimator.SetBool("attacking", false);
sharkAnimator.SetBool("dying", false);
}
function FixedUpdate()
{
///////////
if (provoked == false)
{
if(!Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 1;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, transform.forward, 5))
{
moveDir = 10;
transform.Translate(Vector3.forward * animalSpeed * Time.smoothDeltaTime);
}
if(Physics.Raycast(transform.position, (- transform.right), 1))
{
moveDir = 10;
}
if(Physics.Raycast(transform.position, transform.right, 1))
{
moveDir = -10;
}
transform.Rotate(Vector3.up, 90 * rotSpeed * Time.smoothDeltaTime * moveDir);
}
////////////////////////
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, MaxDist)) {
provoked = true;
Debug.DrawLine (transform.position, hit.point, Color.red);
}
else if (Physics.Raycast (transform.position, transform.forward, hit, loseSight)) {
provoked = false;
Debug.DrawLine (transform.position, hit.point, Color.blue);
}
if (provoked == true)
{
if(Vector3.Distance(transform.position,Player.position) <= MinDist)
{
Attacking ();
}
else if(Vector3.Distance(transform.position,Player.position) <= MaxDist && attacking == false)
{
MoveSpeed = 5;
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
}
if(Vector3.Distance(transform.position,Player.position) >= MaxDist && (Vector3.Distance(transform.position,Player.position) >= MinDist))
{
provoked = false;
}
}
function Attacking ()
{
attacking = true;
if (attacking == true && turning == true)
{
turning = false;
attacking = false;
yield WaitForSeconds(1);
provoked = false;
}
////////////
if (attacking == true)
{
sharkAnimator.SetBool("attacking", true);
MoveSpeed = 20;
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
//turn on attack here
Debug.Log("shark charges you");
yield WaitForSeconds(2);
turning = true;
attacking = false;
}
if (turning == true)
{
yield;
MoveSpeed = 10;
var targetDir = Player.position - transform.position;
var step = 2 * Time.deltaTime;
var newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 2.0);
transform.rotation = Quaternion.LookRotation(newDir);
sharkAnimator.SetBool("attacking", false);
Debug.Log("shark is turning");
yield WaitForSeconds(3);
turning = false;
provoked = false;
}
}
Answer by HarshadK · Dec 31, 2014 at 11:36 AM
You call your Attacking() coroutine using this condition inside FixedUpdate:
if (provoked == true)
{
if(Vector3.Distance(transform.position,Player.position) <= MinDist)
{
Attacking ();
}
}
And first few lines in your coroutine are:
attacking = true;
if (attacking == true && turning == true)
{
turning = false;
attacking = false;
yield WaitForSeconds(1);
provoked = false;
}
Now your coroutine is called each time the provoking is true and you set provoking to false after one second so for one second of duration new instances of this coroutine will be called from FixedUpdate.
The issue is in your conditions and how you are setting the values of your bools that are used to invoke coroutine.
I'm not quite sure what you mean. If your saying the if (attacking == true && turning == true) part is causing the problem, I'm not sure if it is. It was doing the same thing as before before I added that line in, I just entered it in to help make sure that the shark couldn't turn and attack at the same time, and I have it turn provoke off, to help restart it. But it still rapid fires.
Just try to perform a Debug.Log at different places in the code and you'll come to understand the underlying issue.
Your answer
Follow this Question
Related Questions
Custom Yield Instruction for FixedUpdate frames 3 Answers
Coroutoutine doesnt work properly. 3 Answers
Im a bit confused on a simple script 1 Answer
What's FixedUpdate 2 Answers
Buffering button inputs 1 Answer