hi all, i’m lost in yield waitforseconds i try to change the y rotation from +90 to -90 every 5 seconds using an IEnumerator when a boolean (is_attacked) = true. dont know if the IEnum… doesnt work or the way how to set and get the values then in fixedupdate. hope so much someone will help me to get through.
P.S: this yield thing is soooo complicated for beginners.
this is my script:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float thrust;
public Rigidbody rb;
public float yRotation = 0.0F;
public bool is_attacked = false;
public bool turned_left = false;
public bool turned_right = false;
void Start()
{
rb = GetComponent<Rigidbody>();
}
IEnumerator Wait()
{
yield return new WaitForSeconds(5);
}
void Update()
{
if (yRotation >= 360.0F)
{
yRotation = 0.0F;
}
if (yRotation <= -360.0F)
{
yRotation = 0.0F;
}
// yRotation += Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
y_rotation();
transform.eulerAngles = new Vector3(0, yRotation, 0);
rb.AddRelativeForce(Vector3.forward * thrust);
rb.velocity = transform.forward * rb.velocity.magnitude;
}
IEnumerator y_rotation()
{
if (is_attacked == true)
{
yRotation = -90.0f;
yield return StartCoroutine(Wait());
yRotation = 90.0f;
}
}
}