Unity3D
[unity]lookAt 함수 x,z축으로만 보게 만들기
메밀국수가생각나
2020. 5. 20. 22:35
lookAt을 사용해 사용자를 npc가 바라보게 하였다.
그런데 한가지 문제가 발생했다.
npc가 플레이어를 잘 바라본다, 하지만 위를 바라보고 있었다.
하지만 정면을 바라봐야 제대로 되었다 할 수 있기 때문에 정면을 보게끔 코드를 수정하였다.
플레이어의 x,z축만 가져오고 y축은 사용하지 않는 것이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtPlayer : MonoBehaviour
{
public GameObject target;
private Vector3 targetPosition;
private void OnTriggerStay(Collider other)
{
// other.gameObject == target.gameObject
if (other.tag == "Player")
{
targetPosition = new Vector3(other.transform.position.x, transform.position.y, other.transform.position.z);
transform.LookAt(targetPosition);
}
}
}