forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWander.cs
More file actions
42 lines (37 loc) · 1.13 KB
/
Copy pathWander.cs
File metadata and controls
42 lines (37 loc) · 1.13 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
40
41
42
using UnityEngine;
namespace Graphical.AnimationWithGameObjects
{
public class Wander : MonoBehaviour
{
private float m_nextActionTime;
private float m_period;
private Animator m_animator;
private int m_isMovingID;
private void Awake()
{
m_nextActionTime = Time.time;
m_animator = GetComponent<Animator>();
m_isMovingID = Animator.StringToHash("IsMoving");
}
void Update()
{
var timeLeft = m_nextActionTime - Time.time;
if (timeLeft < 0f)
{
m_period = Random.Range(2f, 5f);
m_nextActionTime += m_period;
var angle = Random.Range(0f, 360f);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
else
{
bool isMoving = timeLeft > m_period / 2f;
m_animator.SetBool(m_isMovingID, isMoving);
if (isMoving)
{
transform.position += transform.forward * (Time.deltaTime * 3f);
}
}
}
}
}