using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aim : MonoBehaviour
{[SerializeField]
private Transform _cube;// 直接设置一个空间变量// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){
Vector3 directionToFace = _cube.position - transform.position;// 计算和目标的相对位置
Debug.DrawRay(transform.position, directionToFace, Color.green);// 画出个射线,用于 debug,其实位置为人物位置,然后设定方向,设定颜色
transform.rotation = Quaternion.LookRotation(directionToFace);// 旋转角度设置为面向目标}}
注意点:如果人物的默认 Local 坐标系和 Global 坐标系不匹配,会出问题
可以考虑新建一个空的游戏对象(默认与 Global 坐标系一致),然后作为父对象,把不一致的部分拖进去作为子对象