오브젝트 바라보게하기(Lookat)
오브젝트가 특정 타겟을 기준으로 바라보게 하는 코드이다.
회전방향을 특정 타겟으로 향하게 한다.
< Rotation을 기준으로 처리 >
Vector3 relativePos = target.position - transform.position;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
transform.rotation = rotation;
<Transform LookAt 으로 처리 >
혹은 Transform의 Lookat 코드를 사용해도된다.
using UnityEngine;
public class LookAtTarget : MonoBehaviour
{
// 바라볼 타겟의 위치를 지정합니다.
public Transform target;
void Update()
{
// 타겟 위치가 지정되어 있는 경우에만 동작합니다.
if (target != null)
{
// 타겟 위치를 바라보도록 합니다.
transform.LookAt(target);
}
}
}
UnityLookRotation : [링크]
Unity - Scripting API: Quaternion.LookRotation
Z axis will be aligned with forward, X axis aligned with cross product between forward and upwards, and Y axis aligned with cross product between Z and X. Returns identity if the magnitude of forward is zero. If forward and upwards are colinear, or if the
docs.unity3d.com
Unity Transform.Lookat : [링크]
Unity - Scripting API: Transform.LookAt
Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. The up vector of the rotation will only match the worldUp vecto
docs.unity3d.com
★☆☆☆☆
'개발 > Unity' 카테고리의 다른 글
Unity) txt 파일 생성 및 저장(I/O) (0) | 2020.06.16 |
---|---|
소식)Unity 2019 LTS (1) | 2020.06.14 |
Unity) 워크플로우 속도 향상을 위한 기능 5가지(2019 v) (0) | 2020.06.01 |
Unity) 프로파일러( Profiler ) (0) | 2020.06.01 |
Unity) MeshCollider.convex (0) | 2020.06.01 |
댓글