- Home /
Question by
AmbientGames · Mar 11, 2021 at 10:13 PM ·
animationgameobjectrandom.range
Randomly genrerated movement not working.
Hi! I have a script that randomly moves a animtronic when ever the number it generates is equivelent to the point number. But,the animatronic doesn't move at all. Script:` //Variables, if you're a nerd you would read these. public string nameOfEnemy;
public bool playerIsDead;
public int randomPoint;
public float startTimerTillMove;
private float timerTillMove;
public bool isAtOfficeDoor;
public GameObject Henry;
public GameObject point1;
public GameObject point2;
public GameObject point3;
public GameObject point4;
public GameObject point5;
public GameObject point6;
public AudioSource JumpScareSound;
public Building building;
public void Start()
{
Henry.GetComponent<Animator>().Play("idle");
}
//A timer for when animatronics move. It also makes animatronics go away when you close the door.
private void Update()
{
timerTillMove -= Time.deltaTime;
if (timerTillMove <= 0)
{
randomPoint = Random.Range(1, 3);
GoToPoint();
timerTillMove = startTimerTillMove;
}
if (isAtOfficeDoor == true)
{
if (building.isLeftDoorClosed == true)
{
gameObject.transform.position = point1.transform.position;
}
}
}
//The animtronics can't land on point 4 and 5 anymore, if they reach 3, they must wait 30 seconds before moving to 4 and 5
public IEnumerator GoToPoint()
{
if (randomPoint == 1)
{
gameObject.transform.position = point1.transform.position;
}
if (randomPoint == 2)
{
gameObject.transform.position = point2.transform.position;
}
if (randomPoint == 3)
{
gameObject.transform.position = point3.transform.position;
yield return new WaitForSeconds(30f);
gameObject.transform.position = point4.transform.position;
}
if (randomPoint == 4)
{
gameObject.transform.position = point4.transform.position;
yield return new WaitForSeconds(30f);
gameObject.transform.position = point5.transform.position;
}
if (randomPoint == 5)
{
gameObject.transform.position = point5.transform.position;
isAtOfficeDoor = true;
}
}
//If Jumpscare() is called it will play the animation for it.
public void JumpScare()
{
Henry.GetComponent<Animator>().Play("JumpScareHenry");
playerIsDead = true;
}
} `
Comment
Answer by highpockets · Mar 11, 2021 at 10:29 PM
At first glance I notice that GoToPoint() is an IEnumerator/Coroutine, but you are not calling it correctly. To start a coroutine you have to put StartCoroutine(GoToPoint());
I also noticed that you’re randomPoint is chosen with Random.Range(1,3)
and with ints it will return the first number inclusive - second number exclusive, so it will only return 1 or 2 with the way you have it now