1. 물체를 움직이는 방법

 - tranform.Translate 사용

public class PlayerController : MonoBehaviour
{
       public float speed = 5.0f;
       public float horizontalInput;

       void Start()
       {
 
       } 

       void Update()
       {
             forwardInput = Input.GetAxis("Vertical");
 
             transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
       }
}

 

2. 물체를 움직이는 방법

- rigbody 정보를 받아와서 MovePosition 함수를 사용

public class Rigid2dController : MonoBehaviour
{
       public Rigidbody rb;
       private Vector3 velocity;

       void Awake()
       {
             rb = gameObject.GetComponent<Rigidbody>();   //rigidBody 가져오기
       }
       void Start()
       {
             velocity = new Vector3(0,0,2.0f);                            //속도
             transform.position = new Vector3(6.0f,0,0);         //초기 위치값 설정
       }
 
       void Update()
       {
             rb.MovePosition(rb.position + velocity * Time.deltaTime);
       }
}

 

 

3. 물체를 회전시키는 기본적인 방법

public class PlayerController : MonoBehaviour
{
       public float turnSpeed = 20.0f;
       public float forwardInput;

       void Start()
       {
 
       }

       void Update()
       {
              horizontalInput = Input.GetAxis("Horizontal");
              
              transform.Rotate(Vector3.up,Time.deltaTime * turnSpeed * horizontalInput);
       }
}

+ Recent posts