본문 바로가기

Unity Basic/03. Unity C# script basic

Unity Basic ch. 18] C#Script__Input.GetAxisRaw

키보드를 눌렀을대 방향을 이동하려면 아래와 같이 사용했다.

transform.right

transform.up

transform.forward

 

그런데, 좌우를 한 문자로 해결할 수 있는 예약어가 있다.

좌우=> Horizontal

상하=> Vertical

 

1) Input.GetAxisRaw("Horizontal")

 

Input.GetAxisRaw("Horizontal")

위와 같이 설정하면 왼쪽 방향키를 눌렀을때는 -1을 반환하고 

오른쪽 방향키를 눌렀을때 1을 반환한다.

 

디버그로그를 통해서 좌우 키를 눌렀을때 -1,1값을 반환하는지 알아보자.

 

===================

public class PlayerMove : MonoBehaviour 
{ 
    void Start() 
    { 
        
    } 

    void Update() 
    { 
       Debug.Log(Input.GetAxisRaw("Horizontal");
    }     
}

===================

 

코딩후 플레이하고 좌우 방향버튼을 눌러보자

가만히 있을때는 0 값을 갖고, 오른쪽 화살표를 누르면 1, 왼쪽 화살표를 누르면 -1 값을 갖는다.

 

* 더해보기 

Input.GetAxisRaw("Vertical") 도 찍어보자.

 

 

2) transform.Translate 를 통한 이동

변수를 선언해서

float h; 
float v; 

 

초기 값으로 아래와 같이 넣자 x값, y값으로 값을 넣어보자.

h = Input.GetAxisRaw("Horizontal") ;

v = Input.GetAxisRaw("Vertical");

 

h,v값을 transform.Translate();에 넣자.

 

===================

public class PlayerMove : MonoBehaviour
{
    float h;
    float v;

 

    void Start()
    {
       
    }

    void Update()
    {
          h = Input.GetAxisRaw("Horizontal");
          v = Input.GetAxisRaw("Vertical");
    }
    
    void FixedUpdate()
    {
         transform.Translate(h, v, 0);
    }
}

===================

플레이 버튼을 눌러서 방향키를 눌러보자.

 

 

 

3) 적당한 속력 맞추기

키보드를 누를 때 입력되어야는데 한번 누를 때 너무 많이 감지가 되서 

너무 빨리 지나간다. 여기에서 속력을 낮출수 있게 speed 변수를 public float로 선언해서, 

1이하로 값을 변경해 가면서 적당한 속력으로 맞춰보자.

 

===================

public class PlayerMove : MonoBehaviour
{
    public float speed;
    float h;
    float v;


    void Start()
    {
       
    }

    void Update()
    {
          h = Input.GetAxisRaw("Horizontal")*speed;
          v = Input.GetAxisRaw("Vertical")*speed;
    }
    
    void FixedUpdate()
    {
         transform.Translate(h*speed, v*speed, 0);
    }
}

===================

 

speed를 0.3 정도 주면 적당한 속력으로 이동하는 것을 볼 수 있다. 

 

 

 

4) rigidbody.velocity 를 활용하여 방향제어하기

 

rigidbody를 사용하기위해서 컴포넌트 선언, 초기화를 해야 사용할 수 있다. 

 

===========================

public class PlayerMove : MonoBehaviour
{
    Rigidbody2D rigid;
    public float speed;
    float h;
    float v;

    void Start()
    {
         rigid = GetComponent();
    }

    void Update()
    {
          h = Input.GetAxisRaw("Horizontal")*speed;
          v = Input.GetAxisRaw("Vertical")*speed;
    }
    
    void FixedUpdate()
    {
         rigid.velocity = new Vector3(h,v,0);
    }
}

===========================

 

 

5) Time.deltaTime

 

Time.deltaTime은 프레임 드랍시 밀리는 것을 방지하기 위해 있다.

유니티에서 void update()는 초딩 60번 반복된다. 초당 60프레임이다.

 

사용 방법은 speed옆에 Time.deltaTime을 곱해주면된다.

 

===========================

public class PlayerMove : MonoBehaviour 
{ 
    Rigidbody2D rigid; 
    public float speed; 
    float h; 
    float v; 

    void Start() 
    { 
         rigid = GetComponent(); 
    } 

    void Update() 
    { 
          h = Input.GetAxisRaw("Horizontal")*speed*Time.deltaTime; 
          v = Input.GetAxisRaw("Vertical")*speed*Time.deltaTime; 
    } 
     
    void FixedUpdate() 
    { 
         rigid.velocity = new Vector3(h,v,0); 
    } 
}

===========================