0
\$\begingroup\$

The first script draw numbers above some gameobjects:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;

 public class generatenumbers : MonoBehaviour
 {
     public GameObject[] objectsToNumber;
     public GameObject text;
     public float yPadding;              /// Padding on the Y-Axis.
     public bool rotateNumbers = false;
     public float rotationSpeed = 10f;

     private MeshRenderer[] renderer;
     private Vector3 newPos;
     private List<GameObject> newTexts = new List<GameObject>();

     private void Start()
     {
         renderer = new MeshRenderer[objectsToNumber.Length];

         for (int i = 0; i < objectsToNumber.Length; i++)
         {
             GameObject newText = Instantiate(text);


             renderer[i] = newText.GetComponent<MeshRenderer>();

             newPos = new Vector3
             (
              objectsToNumber[i].transform.position.x,
              ((objectsToNumber[i].transform.position.y + renderer[i].bounds.extents.y) + yPadding),
                objectsToNumber[i].transform.position.z
              );

             newText.transform.position = newPos;
             newText.transform.parent = transform;
             newText.name = i.ToString();
             newText.tag = "Number";
             newTexts.Add(newText);
             var textmesh = newText.GetComponent<TextMesh>();
             textmesh.text = i.ToString();
         }
     }

     private void Update()
     {
         if (rotateNumbers == true)
         {
             for (int i = 0; i < newTexts.Count; i++)
             {

                 newTexts[i].transform.Rotate(Vector3.up, 10 * rotationSpeed * Time.deltaTime);
             }
         }
     }
 }

The result is:

Numbers

The second script make the character/player to move between the waypoints:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class waypoints : MonoBehaviour
 {
     public GameObject[] Waypoints;
     public Transform nextWaypoint;
     public float moveSpeed = 10f;
     public float slowDownSpeed = 3f;
     public float rotationSpeed = 1f;
     public bool random = false;
     public bool reverse = false;

     private int targetsIndex;
     private Vector3 originalPosition;
     private int index;
     private GameObject[] numbers;

     // Use this for initialization
     void Start()
     {
         targetsIndex = 0;
         originalPosition = transform.position;
         index = Random.Range(0, Waypoints.Length);

         numbers = GameObject.FindGameObjectsWithTag("Number");
     }

     // Update is called once per frame
     void Update()
     {
         WayPointsAI();
     }

     private void WayPointsAI()
     {
         if (targetsIndex == Waypoints.Length)
             targetsIndex = 0;

         if (random == true)
         {
             nextWaypoint = Waypoints[index].transform;
         }
         else
         {
             nextWaypoint = Waypoints[targetsIndex].transform;
         }
         float distance = Vector3.Distance(transform.position, nextWaypoint.transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(nextWaypoint.position - transform.position), rotationSpeed * Time.deltaTime);

         if (distance < 3)
         {
             transform.position += transform.forward * slowDownSpeed * Time.deltaTime;
         }
         else
         {
             transform.position += transform.forward * moveSpeed * Time.deltaTime;
         }
         if (distance < nextWaypoint.transform.localScale.magnitude)
         {
             if (random == false)
             {
                 StartCoroutine(tempRotate());
                 targetsIndex++;
             }
             else
             {
                 StartCoroutine(tempRotate());
                 index = Random.Range(0, Waypoints.Length);
             }
         }
     }

     IEnumerator tempRotate()
     {
         while (true)
         {
             if (random == false)
             {
                 numbers[targetsIndex].transform.Rotate(Vector3.up, 10 * 10 * Time.deltaTime);
             }
             else
             {
                 numbers[index].transform.Rotate(Vector3.up, 10 * 10 * Time.deltaTime);
             }

             yield return new WaitForSeconds(3.0f);
         }
     }
 }

I want that each time the character reach a waypoint to rotate for 3 seconds the number object above the waypoint. So i'm trying to use StartCoroutine

StartCoroutine(tempRotate());

Then using while(true) i also tried without the while(true) but nothing make it rotating.

IEnumerator tempRotate()
         {
             while (true)
             {
                 if (random == false)
                 {
                     numbers[targetsIndex].transform.Rotate(Vector3.up, 10 * 10 * Time.deltaTime);
                 }
                 else
                 {
                     numbers[index].transform.Rotate(Vector3.up, 10 * 10 * Time.deltaTime);
                 }

                 yield return new WaitForSeconds(3.0f);
             }
         }

It's like when in game you touch a coin for example and the con rotate a bit for 2-3 seconds then stop. Same idea i want to do with the numbers. When it's reaching to the next waypoint rotate for 3 seconds the number object above this waypoint.

\$\endgroup\$
2
\$\begingroup\$

From what I see in your tempRotate() coroutine, you are rotating the object once and then waiting 3 seconds so clearly you woulnd't be able to see it rotate. What you should do instead is store a float that increments with Time:

float timeElapsed;

void Update(){
    timeElapsed += Time.deltaTime;
}

Now when you call your coroutine, you can set that float to 0 so that u can track how much time has passed, like this :

IEnumerator tempRotate()
    {
        timeElapsed = 0;

        while(timeElapsed <= 3) // this checks if 3 seconds is up
        {
            transform.Rotate(Vector3.up, 10);
            yield return new WaitForEndOfFrame();
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .