物体朝向摄像机
Posted alps_01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了物体朝向摄像机相关的知识,希望对你有一定的参考价值。
锁定物体的X轴旋转
========================================
using UnityEngine;
using System.Collections;
public class LookAtMainCamera : MonoBehaviour
{
public bool LookMode = false;
void Update ()
{
var cameraPos = Camera.main.transform.position;
var objPos = this.transform.position;
var lookV = cameraPos - objPos;
if (LookMode)
{
var crossValue = Vector3.Cross(Vector3.up, lookV);
var crossThis = Vector3.Cross(this.transform.up, crossValue);
this.transform.localRotation = Quaternion.LookRotation(-crossThis, Vector3.up);
} else
{
this.transform.LookAt(cameraPos, Vector3.up);
}
}
}
直接转向插值 或直接lookAt
====================================================
using UnityEngine;
public class LookAtCamera : MonoBehaviour {
public float lerpFactor = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//transform.LookAt(Camera.main.transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(Camera.main.transform.position - transform.position), lerpFactor * Time.deltaTime);
}
}
以上是关于物体朝向摄像机的主要内容,如果未能解决你的问题,请参考以下文章
unity3d中,怎样让鼠标点击一个物体后,摄像机就去拍摄这个物体?