# Vector3
벡터는 위치 + 방향 개념이다
- 벡터를 위치로 사용하는 방법
x, y, z 값을 가지고 있는 벡터는 기본적으로 위치를 나타낸다
Vector3 pos = new Vector(0.0f, 1.0f, 0.0f);
- 벡터를 방향으로 사용하는 방법
방향벡터는 거리(크기)와 실제 방향에 대한 정보를 가지고 있다
magnitude를 통해 거리(크기)
normalized를 통해 magnitude가 1인 단위벡터를 추출해서 실제 방향을 알아낼 수 있다
Vector3 targetPosition = new Vector3(5, 0, 10);
Vector3 currentPosition = transform.position;
// 방향 벡터
Vector3 direction = targetPosition - currentPosition;
// 크기 (거리)
float distance = direction.magnitude;
// 방향 (단위 벡터)
Vector3 unitDirection = direction.normalized;
Debug.Log("방향 벡터: " + direction);
Debug.Log("거리 (magnitude): " + distance);
Debug.Log("실제 방향 (normalized): " + unitDirection);
// 예시로 방향을 따라 이동해보기
transform.position += unitDirection * Time.deltaTime * 3f; // 3f는 속도
# Rotation
오브젝트를 회전하도록 만드는 방법은
아래와 같은 다양한 방법으로 구현할 수 있다
방법 1) transform.eulerAngles 방식 (절대 회전값)
유니티 측에서 eulerAngles 값을 내부에서 계속 수정하면
오류발생 위험성이 있다고 문서를 통해 밝혔다
그렇기에 계산을 외부에서 따로 빼서 하고 set을 해야 한다
float _yAngle = 0.0f;
void Update()
{
_yAngle += Time.deltaTime * 10.0f;
transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
}
방법 2) transform.Rotate 방식 (+- delta)
내부에서 계산하는 방식
void Update()
{
transform.eulerAngles = new Vector3(0.0f, Time.deltaTime * 10.0f, 0.0f);
transform.Rotate(new Vector3(0.0f, Time.deltaTime * 10.0f, 0.0f));
}
방법 3)
Quaternion.Euler 방식
1번과 2번 방식들은 오일러각으로 계산을 하는 방식이다
반면 transform.rotation은 Quarternion 타입이다
그렇기에 인자에 오일러각 Vector3을 넣어 Quaternion으로 변환해
작동시키면 된다
float _yAngle = 0.0f;
void Update()
{
_yAngle += Time.deltaTime * 10.0f;
transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
}
# Rotation 보완
특정 방향으로 회전하도록 수정을 하고 싶다면
Quaternion.LookRotation을 활용하면 된다
transform.rotation = Quaternion.LookRotation(Vector3.forward);
위 코드는 바로 회전을 하기 때문에
천천히 회전하도록 하려면 아래와 같이 Quaternion.Slerp
혹은 Quaternion.RotateTowards를 활용하면 된다
방법 1)
마지막 인자인 float은 0 ~ 1 사이의 값을 받아야 하기에
Time.DeltaTime을 곱하고 Clamp01로 잘라주면 된다
public float rotationSpeed = 2.0f;
public Quaternion targetRotation;
void Update()
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
targetRotation,
Mathf.Clamp01(rotationSpeed * Time.deltaTime)// 클램프된 값 사용
);
}
방법 2)
Clamp01을 사용하는 것이 내키지 않는 다면
RotateTowards를 활용하면 된다
public float anglePerSecond = 100f;
void Update()
{
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRotation,
anglePerSecond * Time.deltaTime);
}
# Rotation 코드 수정
이동과 회전 둘 다 적용이 되어 문제가 있기에 -> 모두 forward로 바꿔주기
현재 로컬 방향과 상관없이 위치를 이동시키고 싶기에 -> Translate방식 말고 transform.position 방식
바라보는 방향과 연관이 없기에 -> 위에서 모두 forward로 바꾼걸 다시 원래대로
float _rotationSpeed = 0.2f;
float _positionSpeed = 10.0f;
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(Vector3.forward,
Mathf.Clamp01(_rotationSpeed * Time.deltaTime));
transform.position += Vector3.forward * Time.deltaTime * _positionSpeed;
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(Vector3.back,
Mathf.Clamp01(_rotationSpeed * Time.deltaTime));
transform.position += Vector3.back * Time.deltaTime * _positionSpeed;
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(Vector3.left,
Mathf.Clamp01(_rotationSpeed * Time.deltaTime));
transform.position += Vector3.left * Time.deltaTime * _positionSpeed;
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(Vector3.right,
Mathf.Clamp01(_rotationSpeed * Time.deltaTime));
transform.position += Vector3.right * Time.deltaTime * _positionSpeed;
}
}
'게임 엔진 - 유니티 > [루키스] 유니티' 카테고리의 다른 글
[유니티 Note] 섹션 4-1 (0) | 2025.06.25 |
---|---|
[유니티 Note] 섹션 3-4 (0) | 2025.06.25 |
[유니티 Note] 섹션 3-2 (0) | 2025.06.24 |
[유니티 Note] 섹션 3-1 (0) | 2025.06.24 |
[유니티 Note] 섹션 2-3 (0) | 2025.06.23 |