- player.cs 의 스크립트를 정리하자!
아래 스크립트는 정리 전의 스크립트 파일입니다.
public class Player : MonoBehaviour
{
public float speed;
float hAxis;
float vAxis;
bool wDown;
Vector3 moveVec;
Animator anim;
void Awake(){
anim = GetComponentInChildren<Animator>();
}
void Update(){
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
wDown = Input.GetButton("Walk");
moveVec = new Vector3(hAxis, 0, vAxis).normalized;
transform.position += moveVec * speed * (wDown ? 0.3f : 1f) Time.deltaTime);
anim.SetBool("isRun", moveVec != Vector3.zero);
anim.SetBool("isWalk", wDown);
transform.LookAt(transform.position + moveVec);
}
}
이제 분리, 정리를 해봅시다
public class Player : MonoBehaviour
{
public float speed;
float hAxis;
float vAxis;
bool wDown;
Vector3 moveVec;
Animator anim;
void Awake(){
anim = GetComponentInChildren<Animator>();
}
void Update(){
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
wDown = Input.GetButton("Walk");
moveVec = new Vector3(hAxis, 0, vAxis).normalized;
transform.position += moveVec * speed * (wDown ? 0.3f : 1f) Time.deltaTime);
anim.SetBool("isRun", moveVec != Vector3.zero);
anim.SetBool("isWalk", wDown);
transform.LookAt(transform.position + moveVec);
}
void GetInput(){
}
void Move(){
}
void Turn(){
}
}
public class Player : MonoBehaviour
{
public float speed;
float hAxis;
float vAxis;
bool wDown;
Vector3 moveVec;
Animator anim;
void Awake(){
anim = GetComponentInChildren<Animator>();
}
void Update(){
GetInput(); // GetInput 메서드가 Update의 가장 위에 와야합니다 중요!
Move();
Turn();
}
void GetInput(){
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
wDown = Input.GetButton("Walk");
}
void Move(){
moveVec = new Vector3(hAxis, 0, vAxis).normalized;
transform.position += moveVec * speed * (wDown ? 0.3f : 1f) Time.deltaTime);
anim.SetBool("isRun", moveVec != Vector3.zero);
anim.SetBool("isWalk", wDown);
}
void Turn(){
transform.LookAt(transform.position + moveVec);
}
}
객체별로 분리
이것이 객체 지향형 프로그래밍!
출처 - https://youtu.be/eZ8Dm809j4c?list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2
'C' 카테고리의 다른 글
3D 쿼터뷰 액션게임 - 플레이어 회피기 (0) | 2023.06.02 |
---|---|
3D 쿼터뷰 액션게임 - 플레이어 점프 (0) | 2023.06.02 |
3D 쿼터뷰 액션게임 - 플레이어 이동 애니메이션, 카메라 이동 (0) | 2023.05.18 |
3D 쿼터뷰 액션게임 - 플레이어 이동 (0) | 2023.05.18 |
unity Vector 수학( 내적 Dot, 외적 Cross ) (0) | 2023.05.16 |