-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimalAI.cs
More file actions
39 lines (35 loc) · 1.11 KB
/
animalAI.cs
File metadata and controls
39 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animalAI : MonoBehaviour {
public float distance = 10f;
private Vector3 cPos; //Current Position
public float speed = 4.0f;
private Vector3 targetPos;
private bool waitformove = false;
void Start()
{
}
void Update()
{
if (waitformove == false) {
cPos = this.transform.position;
targetPos = new Vector3(cPos.x + Random.Range(-distance /2,distance / 2), cPos.y, cPos.z + Random.Range(-distance / 2, distance / 2));
print(targetPos);
transform.LookAt(targetPos);
waitformove = true;
}
if (waitformove)
{
Vector3 d = targetPos - transform.position;
Vector3 movement = d.normalized * speed * Time.deltaTime;
transform.position += movement;
if (movement.sqrMagnitude > d.sqrMagnitude)
{
// reached the target position
transform.position = targetPos;
waitformove = false;
}
}
}
}