@ 방향키를 누르면 이동하기
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()메서드 없으면 그냥 돌고, 있으면 어떤 물체를 계속 바라보며 공전함.
카메라(Camera) (0) | 2024.06.25 |
---|---|
MeshRenderer, 마우스 클릭시 오브젝트 색 바꾸기 (0) | 2024.06.25 |
박스 콜라이더, 레이캐스트, OnTriggerEnter, OnCollisionEnter (0) | 2024.06.25 |
리지드바디(Rigidbody), 이동, 회전, 폭발 (0) | 2024.06.24 |
스크립트의 직렬화(SerializeField / Serializable) (0) | 2024.05.30 |