Unity 模型始终朝向某物体(LookAt+Rotate)

目录:

        • 问题
        • 解决
        • 注意

问题

  transform.LookAt(目标物体的transform) 是让当前模型的Z轴指向目标物体位置,但是模型的Z轴不一定是它的正面朝向。

解决

  法一,给模型加空父物体,把模型的正面转向父物体Z轴,让父物体的Z轴朝向目标物体。
  法二,模型Z轴直接朝向父物体,再对模型进行旋转,使其Z轴指向目标物体。
     示例:让立方体的Y轴朝向目标物体主摄像机
Unity 模型始终朝向某物体(LookAt+Rotate)_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComprehesionRotate : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(Camera.main.transform);
        
        transform.Rotate(new Vector3(90, 0, 0));
        
    }
}

Unity 模型始终朝向某物体(LookAt+Rotate)_第2张图片

注意

  "法二"是存在一定的误差,并不是真的像代码所写,围绕X轴转了90度
Unity 模型始终朝向某物体(LookAt+Rotate)_第3张图片

你可能感兴趣的:(UnityAPI)