- Home /
Question by
MartinAstrab32 · Apr 24 at 07:21 PM ·
beginner
How can i make it turn only for x seconds?
public float speed = 3f;
public float resetTimer = 1f;
public float maxRot = 0f;
Vector3 direction = Vector3.forward;
public void Start()
{
}
public void Update()
{
float timer = Random.Range(0, 3000);
Vector3 drift = transform.position;
drift += direction * speed * Time.deltaTime;
transform.position = drift;
transform.Rotate(maxRot, 0, 0);
if (timer > 0)
{
timer -= Time.deltaTime;
}
else
{
Debug.Log("turn");
timer = resetTimer;
if (direction == Vector3.forward)
{
direction = Vector3.back;
}
else
{
direction = Vector3.forward;
}
}
}
}
Comment
Answer by graydogstudios · Apr 24 at 08:58 PM
You got the problem backwards.
while (timer>0) transform.Rotate(maxRot, 0, 0); timer--;
This will turn for timer duration of time.
I would also recommend using Time.deltatime instead so your turn isnt frame dependnt.