Spawning object with new random pathing.,Randomly moving after spawn
Hey. I am trying to make my animals(chickens) to move randomly, independently from one another in restricted area. Only thing i found was code below. Everything works fine but when i would like to implement spawning new chicken with same script, they both follow same route. Is there a way to spawn new chicken and script randomizing different wandering position than first one? Or is there a better way of creating random movement in area? Or problem is with way i spawn my animal? When i Instantiate new chicken on mouse click it spawns new one who follows exactly same path.
[SerializeField] public float speed;
private float waitTime;
[SerializeField] public float startWaitTime;
public Transform moveSpot;
[SerializeField] public float minX;
[SerializeField] public float maxX;
[SerializeField] public float minY;
[SerializeField] public float maxY;
// Start is called before the first frame update
void Start()
{
waitTime = startWaitTime;
moveSpot.position = new Vector2(Random.Range(minX,maxX), Random.Range(minY,maxY));
}
// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position,moveSpot.position, speed * Time.deltaTime);
if(Vector2.Distance(transform.position,moveSpot.position) < 0.005f) { //added array
if(waitTime <= 0) {
moveSpot.position = new Vector2(Random.Range(minX,maxX), Random.Range(minY,maxY)); //added array
waitTime = startWaitTime;
} else {
waitTime -= Time.deltaTime;
}
}
}
Your answer
Follow this Question
Related Questions
How can I let my car AI follow a Node-Path slightly random? (C#) 0 Answers
Can no1 help me??? 1 Answer
Trajectory shooting Tower! 0 Answers
Hello there I have a Instantiate Issue 0 Answers
Top down space AI pathing? 0 Answers