상세 컨텐츠

본문 제목

Transform 오브젝트 이동하기, 회전하기, 크기조절, 바라보기, 공전하기

유니티/기능

by MJ_119 2024. 6. 22. 15:35

본문

@ 방향키를 누르면 이동하기

Translate() 메서드 사용.

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

public class Test : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            방법1
            this.transform.position += new Vector3(0, 0, 2) * Time.deltaTime;
            
            방법2
            this.transform.Translate(new Vector3(0,0,3) * Time.deltaTime);
            
            방법3
            this.transform.Translate(Vector3.right * Time.deltaTime);
            
            방법4
            float speed = 3.0f;
            this.transform.Translate(Vector3.right * speed * Time.deltaTime);
        }
    }
}

 

Vector3.right, Vector3.up, Vector3.down, Vector3.left, Vector3.forward, Vector3.back 

 - 이러한 정적 속성들은 미리 정의되어 있기 때문에 new 키워드를 사용할 필요가 없습니다.

 

@ 방향키를 누르면 회전하기
- Rotate() 메서드 사용.

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

public class Test : MonoBehaviour
{
    Vector3 rotation;

    private void Start()
    {
        rotation = this.transform.eulerAngles;
    }
    
    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            rotation += new Vector3(90, 0, 0) * Time.deltaTime;
            this.transform.eulerAngles = rotation;
			
            // 아래와 같이 하면 x : 90 넘으면 오류 발생
            //this.transform.eulerAngles += new Vector3(90, 0, 0) * Time.deltaTime;
        }
    }
}

 

if(Input.GetKey(KeyCode.W))
        {
            this.transform.Rotate(new Vector3(90, 0, 0) * Time.deltaTime);
        }

 

- 짐벌락 현상이 발생할것 같으면 rotation 사용.

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

public class Test : MonoBehaviour
{
    Vector3 rotation;

    private void Start()
    {
        rotation = this.transform.eulerAngles;
    }
    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            rotation = rotation + new Vector3(90, 0, 0) * Time.deltaTime;
            this.transform.rotation = Quaternion.Euler(rotation);
        }
    }
}

 

@ 크기조절

- localScale 사용.

if(Input.GetKey(KeyCode.W))
        {
            this.transform.localScale += new Vector3(2, 2, 2) * Time.deltaTime;
        }

 

@ 바라보기

 - Lookat() 메서드 사용

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

public class Test : MonoBehaviour
{
    [SerializeField]
    private GameObject Look_camera;

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            this.transform.LookAt(Look_camera.transform.position);
        }
    }
}

 

[SerializeField]  : private 속성이어도 인스펙터창에서 보이게 할 수 있음.
    private GameObject Look_camera;

인스펙터창에서 바라볼 오브젝트를 드래그해서 넣어줘야 동작함.

 

@ 오브젝트, 물체 주위를 공전하기

RotateAround() 메서드

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

public class Test : MonoBehaviour
{
    [SerializeField]
    private GameObject Look_camera;

    void Update()
    {
        if(Input.GetKey(KeyCode.W))
        {
            this.transform.LookAt(Look_camera.transform.position);
            this.transform.RotateAround(Look_camera.transform.position, Vector3.up, 100 * Time.deltaTime);
        }
    }
}

 

 

LookAt()메서드 없으면 그냥 돌고, 있으면 어떤 물체를 계속 바라보며 공전함.

 

관련글 더보기