我正在尝试使相机围绕播放器旋转,以使播放器始终位于屏幕中间。
我尝试使用Slerp()
函数。
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour
{
public Transform target;
public float Speed = 1f;
public Camera cam;
public Vector3 offset;
void Update()
{
Vector3 direction = (target.position - cam.transform.position).normalized;
Quaternion lookrotation = target.rotation;
Quaternion playerrotation = target.rotation;
playerrotation.y = target.rotation.y;
playerrotation.x = 0f;
playerrotation.z = 0f;
lookrotation.x = transform.rotation.x;
lookrotation.z = transform.rotation.z;
//lookrotation.y = transform.rotation.y;
offset.x = -target.rotation.x * Mathf.PI;
transform.rotation = Quaternion.Slerp(transform.rotation, playerrotation, Time.deltaTime * Speed);
transform.position = Vector3.Slerp(transform.position, target.position + offset, Time.deltaTime * 10000);
}
}
可以,但是播放器不在屏幕中间。
答案 0 :(得分:0)
以下脚本将旋转与其相连的gameObject,以将Target
gameObject保持在屏幕中央,并使摄像机的视线与目标方向相同。
public Transform Target;
public float Speed = 1f;
public Vector3 Offset;
void LateUpdate()
{
// Compute the position the object will reach
Vector3 desiredPosition = Target.rotation * (Target.position + Offset);
// Compute the direction the object will look at
Vector3 desiredDirection = Vector3.Project( Target.forward, (Target.position - desiredPosition).normalized );
// Rotate the object
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( desiredDirection ), Time.deltaTime * Speed );
// Place the object to "compensate" the rotation
transform.position = Target.position - transform.forward * Offset.magnitude;
}
注意:除非您真的知道自己在做什么,否则永远不要,永远不要永远。四元数不存储您在检查器中看到的值。它们是用于表示旋转的复杂实体,具有4个值。