Husky_script.cs
2.43 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Husky_script : MonoBehaviour
{
[SerializeField] private string animalName;
[SerializeField] private float walkSpeed;
private Vector3 direction; // 방향
// 상태변수
private bool isAction; // 행동중인지 아닌지 판별
private bool isWalking; // 걷는지 안 걷는지 판별
[SerializeField] private float walkTime; // 걷기 시간
[SerializeField] private float waitTime; // 대가 시간
private float currentTime;
// 필요한 컴포넌트
[SerializeField] private Animator anim;
[SerializeField] private Rigidbody rigid;
[SerializeField] private BoxCollider boxCol;
// Start is called before the first frame update
void Start()
{
currentTime = waitTime;
isAction = true;
}
// Update is called once per frame
void Update()
{
Move();
Rotation();
ElapseTime();
}
private void Move()
{
if (isWalking)
rigid.MovePosition(transform.position + (transform.forward * walkSpeed * Time.deltaTime));
}
private void Rotation()
{
if(isWalking)
{
Vector3 _rotation = Vector3.Lerp(transform.eulerAngles, direction, 0.01f);
rigid.MoveRotation(Quaternion.Euler(_rotation));
}
}
private void ElapseTime()
{
if(isAction)
{
currentTime -= Time.deltaTime;
if (currentTime <= 0)
ReSet(); // 다음 랜덤 행동 개시
}
}
private void ReSet()
{
isWalking = false; isAction = true;
anim.SetBool("Walking", isWalking);
direction.Set(0f, Random.Range(0f, 360f), 0f);
RandomAction();
}
private void RandomAction()
{
int _random = Random.Range(0, 3); // 대기, 두리번, 걷기
if(_random == 0)
Wait();
else if(_random == 1)
Peek();
else if (_random == 2)
TryWalk();
}
private void Wait()
{
currentTime = waitTime;
Debug.Log("대기");
}
private void Peek()
{
currentTime = waitTime;
anim.SetTrigger("Peek");
Debug.Log("두리번");
}
private void TryWalk()
{
isWalking = true;
anim.SetBool("Walking", isWalking);
currentTime = walkTime;
Debug.Log("걷기");
}
}