728x90
반응형
오브젝트 이동 메서드
transform.Translate() 메서드를 이용하여 오브젝트를 이동시킬 수 있다.
아래 코드를 오브젝트에 적용시킨 후 실행시키면 해당 오브젝트가 우측으로 움직이는 것을 볼 수 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(0.01f,0,0);
}
}
transform.Translate() 메서드의 각각의 인자는 x, y, z 좌표를 의미하며 인자로 받은 값만큼 오브젝트를 이동시키는 역할을 한다.
따라서 위 코드는 Update()가 반복될 때마다 오브젝트의 x좌표를 0.01만큼 이동시키는 동작을 한다.
여기서 주의해야 하는 점이 있는데 Vector3 클래스에 사용된 자료형은 float형이기 때문에
this.transform.Translate(0.01,0,0) 이라고 작성하게 되면 자료형 불일치로 오류가 발생한다.
오브젝트 회전 메서드
transform.Rotate 메서드를 사용하면 오브젝트를 회전시키는 것도 가능하다.
Rotate 메서드에서 인자로 수를 전달하면 전달된 각도만큼 각 축을 기준으로 회전한다.
따라서 아래 코드를 실행시켜보면 키보드의 'w' 키를 누를 때마다 오브젝트가 z 축을 기준으로 30도씩 회전하는 것을 볼 수 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
this.transform.Rotate(0, 0, 30);
}
}
728x90
반응형
'유니티 > 유니티 엔진' 카테고리의 다른 글
[유니티] 이미지 반전 (transform.localScale) (0) | 2023.10.07 |
---|---|
[유니티] GetComponent, AddForce (0) | 2023.10.06 |
[유니티] 물리엔진, 충돌 (Rigidbody, Collider, OnCollisionEnter) (0) | 2023.10.06 |
[유니티] 오브젝트의 좌표 (transform.position) (0) | 2023.10.06 |
[유니티] 키보드, 마우스 입력 처리 함수 (0) | 2023.10.06 |