我用rotate和lookat控制跟随旋转,目标旋转超过90度时都会出现这个问题,旋转超过90度之前
90度后
挂到相机上面的脚本挂在目标物体上的脚本
http://blog.csdn.net/alayeshi/article/details/46804371
父对象不要有缩放 保持原缩放即可,就是x=1;y=1;z=1
那是因为你目标旋转也超过了90度,目标物体倒过来了,所以摄像机也跟着倒过来了。想不让摄像机倒过来的话就限定目标x轴旋转角度的范围。一般来说向后旋转这个操作都是绕y轴转达到的而不是绕x轴转过去的。摄像机好比人的眼睛,想看后面你翻个跟头去看当然是倒着的,只要回头就好了……
我做过一个ui面向摄像机的,出现倒置问题,最后用法线去算的,你可以参考一下
http://blog.csdn.net/n_moling/article/details/79205836
你跟随的方式是哪一种呀?
你好,
我想象了一下你的问题放生的情况。游戏刚开始时,你的飞机坐标系(local)和世界坐标系是平行的。即x,y,z各个轴的方向是一样。
假设飞机绕自身y轴顺时针旋转90度,再绕x轴逆时针旋转90度后,飞机坐标系的x,y,z轴分别朝着世界坐标系的-z,-x,y轴。
这个时候你的飞机是沿着世界坐标系的Y轴方向前进。而你的镜头跟随代码却要求每一帧,镜头位置比目标点位置高一些,然后,lookat方法回让镜头回头看目标点。
也就是说,你的镜头位置一直在飞机前面,摄像机每一帧都在前面回头,也就是镜头翻转。
public float distance = 10;//跟随距离
public float height = 5;//跟随高度
public float heightDamping = 2;//高度阻尼
public float rotateDamping = 3;//角度阻尼
public float heightOffet = 1;//高度偏移量
public Transform target;
// Use this for initialization
void Start () {
}
private void LateUpdate()
{
if (target != null)
{
float wantRotationAngle = target.eulerAngles.y;//目的角度
float wantHeight = target.position.y + height;//目的高度
float currentRotationAngle = transform.eulerAngles.y;//当前角度
float currentHeight = target.position.y + height;//当前高度
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantRotationAngle, rotateDamping * Time.deltaTime);//角度渐变到目的角度
currentHeight = Mathf.Lerp(currentHeight, wantHeight, heightDamping * Time.deltaTime);//高度渐变到目的高度
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
transform.position = target.position;//位置调整
transform.rotation = currentRotation;
transform.position -= transform.forward * distance;
Vector3 currentPosition = transform.position;
currentPosition.y = currentHeight;//设置照相机高度
transform.position = currentPosition;//设置摄像机位置
transform.LookAt(target.position + new Vector3(0, heightOffet, 0));
}
}
你在运行之前把相机的Position调到0,0,0试试,这个可能回会影响到摄像机的正常旋转
可以检查下,手机或是设备有没有开启跟随重力自动旋转,如果有,先将设置去掉,再试下