Hey guys,
read and watched a good number of tutorials I still seem to be missing something that makes this work properly. Any help would be appreciated.
public class MischiefMonkey : MonoBehaviour
{
[SerializeField]
private float _speed = 7f;
[SerializeField]
private Transform[] _treeTopLocations;
void Update()
{
//stay at current locations for 2 seconds
//move to new location
//destroy object after 15 seconds
StartCoroutine(MonkeyMovement());
}
IEnumerator MonkeyMovement()
{
while (GameManager.gameManager.gameOver == false)
{
if (UIManager.uIManager.pauseMenuVisible == true)
{
yield return null;
}
else
{
Transform newTree = TreeTopLocations();
yield return new WaitForSeconds(2);
transform.Translate(newTree.position * _speed * Time.deltaTime);
}
}
}
public Transform TreeTopLocations()
{
int randomIndex = Random.Range(0, _treeTopLocations.Length);
return _treeTopLocations[randomIndex];
}
}
also not entirely sure where to put the destroy object but im less worried about that.
Ok you have two main problems going on. Your update loop and your translate function.
The update loop runs every frame. You are starting a coroutine every frame. That coroutine loops infinitley while you are playing the game. So you are getting an infinite amount of inifite looping code. That will get you some crazy results. Instead you should be starting that coroutine on Awake or Start. Secondly it looks like your translate function is not used properly. You find newTree’s position. but then you multiply that position by speed and Time.deltaTime. So you are going to move the object a tiny bit every 2 seconds and not to the actual found position. I suggest doing transform.position = newTree.position. That would instantly teleport the object to the found location. If you get that working then you can figure out how you want to move the object over time instead. Here is what I think you are trying to do in code. I haven’t run it so it may have some typos:
using System.Collections;
using UnityEngine;
public class MischiefMonkey : MonoBehaviour {
[SerializeField]
private float _speed = 7f;
private float _closeThreshold = .1f;
[SerializeField]
private Transform[] _treeTopLocations;
//The position we want to move to
private Vector3 _targetPos;
void Awake() {
//Set our first target location to where we already are
_targetPos = transform.position;
//Startup your looping code that finds the target location every few seconds
StartCoroutine(MonkeyMovement());
}
void Update()
{
//If we aren't close to our target location
if(Vector3.Distance(transform.position, _targetPos) > _closeThreshold)){
//Find the direction from where we are to where we want to be
//Normalize it to get a direction rather than the whole distance
Vector3_currentDir = (_targetPos - transform.position).normalized;
//Move towards the target
transform.Translate(_currentDir * _speed * Time.deltaTime);
}
}
IEnumerator MonkeyMovement()
{
while (GameManager.gameManager.gameOver == false)
{
if (UIManager.uIManager.pauseMenuVisible == true)
{
yield return null;
}
else
{
//Dont get a new target for 2 seconds
yield return new WaitForSeconds(2);
//Get a new target
Transform newTree = TreeTopLocations();
_targetPos = newTree.position;
}
}
}
public Transform TreeTopLocations()
{
int randomIndex = Random.Range(0, _treeTopLocations.Length);
return _treeTopLocations[randomIndex];
}
}
If you wanna move the Monkey towards the target position that you just got from an Array using Translate the you must get a direction Vector pointing towards the target from your current position => (target.transform.position - transform.position).normalize. We normalize to get the magnitude as 1 so you can provide your own speed to manipulate the movement towards a point.
I would suggest you to make a targetfield for monkey and assign that target field from the array and move in the update function towards the target point.
You can put the Destroy Code in making a Timer using IEnumerator or you can do it in Update Loop too.
Hope this helps!