我想旋转并跟随摄像机,并且x和y轴的对象都是我,但是它仅绕单轴旋转并且旋转不正确。
public class CameraController : MonoBehaviour {
public float turnSpeed = 2.0f;
public Transform player;
private float x, y = 0;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
// Place the script in the Camera-Control group in the component menu
[AddComponentMenu("Camera-Control/Smooth Follow")]
void Start () {
}
void LateUpdate()
{
if (!player) return;
// Calculate the current rotation angles
float wantedRotationAngle = player.eulerAngles.y;
float wantedHeight = player.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = player.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x,currentHeight,transform.position.z);
#if UNITY_STANDALONE || UNITY_WEBGL || UNITY_EDITOR
if (Input.GetMouseButton(0))
{
x = Mathf.Lerp(player.position.x, Mathf.Clamp(Input.GetAxis("Mouse X"), -2, 2) * turnSpeed, Time.deltaTime * 5.0f);
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 60, 60);
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime);
}
else {
x = Mathf.Lerp(player.position.x, turnSpeed * 0.01f, Time.deltaTime * 5.0f);
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime);
}
#elif UNITY_ANDROID||UNITY_IOS
if (Input.touchCount == 1)
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Moved:
x = Mathf.Lerp(x, Mathf.Clamp(Input.GetTouch(0).deltaPosition.x, -2, 2) * cameraRotateSpeed, Time.deltaTime*3.0f);
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 60, 60);
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime);
break;
}
}
else {
x = Mathf.Lerp(x, cameraRotateSpeed * 0.02f, Time.deltaTime*3.0f);
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime);
}
#endif
transform.RotateAround(player.position, Vector3.up, x);
transform.LookAt(player);
}
}
相机必须跟随并旋转并平稳地对准对象。需要建议