【发布时间】:2018-05-12 11:58:06
【问题描述】:
我做了一个简单的赛车游戏。相机跟随汽车(玩家),它的位置和旋转基于汽车的 Y 旋转。我想平滑相机旋转,但是当它越过 0 度点时,它会旋转 360 度。 代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour {
public Transform camera, player;
void Update() {
Vector3 cameraPosition = camera.transform.position;
float cameraRotation = camera.eulerAngles.y;
float playerRotation = player.eulerAngles.y;
Vector3 playerPosition = player.transform.position;
cameraPosition.x = (Mathf.Sin((playerRotation / 180) * Mathf.PI) * -6 + player.position.x);
cameraPosition.y = playerPosition.y + 2.5f;
cameraPosition.z = (Mathf.Cos((playerRotation / 180) * Mathf.PI) * -6 + player.position.z);
camera.transform.position = cameraPosition;
cameraRotation = cameraRotation + (playerRotation-cameraRotation)/2;
camera.localRotation = Quaternion.Euler(20f, cameraRotation, 0f);
}
}
我发现这种旋转是由平滑脚本引起的:
cameraRotation = cameraRotation + (playerRotation-cameraRotation)/2;
如何防止这种不必要的旋转?
【问题讨论】:
-
旋转基于什么样的公式?你想让相机看车吗?另外,为什么不把摄像头放在车上,这样你就不用搞乱Sin和Cos了?
-
相机的行为几乎像个孩子,但我想添加这种平滑以使它看起来更好一点。是的,我想让相机看汽车。 Sin 和 Cos 没有问题,但平滑脚本。它看起来像这样:video
-
另外,您可以使用
cameraRotation = cameraRotation + (playerRotation-cameraRotation)/10;使其更慢且更显眼。 -
我知道 Sin 和 Cos 没有问题。在我看来这只是不必要的,因为您只需将相机作为汽车的孩子即可获得类似的效果