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:
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.