锁定物体的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);
}
}