유니티/AA(3D 쿼터뷰 액션 게임)

AA(2) - 코드 정리

무직백수취업준비생 2021. 8. 30. 20:52
728x90
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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();
        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 * Time.deltaTime;

        if (wDown)
            transform.position += moveVec * speed * 0.01f * Time.deltaTime;
        else
            transform.position += moveVec * speed * Time.deltaTime;

        anim.SetBool("isRun", moveVec != Vector3.zero);
        anim.SetBool("isWalk", wDown);
    }

    void Turn()
    {
        transform.LookAt(transform.position + moveVec);
    }
}

업데이트에 나열된 함수들을 정리해줍니다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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();
        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.01f : 1f) * Time.deltaTime;


        anim.SetBool("isRun", moveVec != Vector3.zero);
        anim.SetBool("isWalk", wDown);
    }

    void Turn()
    {
        transform.LookAt(transform.position + moveVec);
    }
}

Move()를 삼항연산자로 정리해줍니다.

 

플레이해보면서 speed값과 애니메이션 재생속도를 조절해줍니다.

728x90
반응형

'유니티 > AA(3D 쿼터뷰 액션 게임)' 카테고리의 다른 글

AA(7) - 캐릭터 변경하기  (0) 2021.09.10
AA(5) - 아이템 만들기  (0) 2021.09.08
AA(4) - 플레이어 회피  (0) 2021.09.01
AA(3) - 플레이어 점프  (0) 2021.08.30
AA(1) - 플레이어 이동  (0) 2021.08.29