본문 바로가기

C

3D 쿼터뷰 액션게임 - 코드 정리

 

 

  • 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