(适用于Unity 5.3.5f1) 现在我正在制作一个围绕播放器水平绕轨道运行的3D相机。玩家是RigidBody滚动球体。玩家可以自由地水平旋转轴,但是当没有输入时,我希望旋转重置回速度方向。
现在我需要知道的是如何通过在前一次旋转时围绕播放器旋转相机,将相机置于播放器的速度方向后面。
目前要将相机更新到播放器周围的轨道我在相机上使用此脚本(带注释):
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject Player;
//I assume you would use this PlayerRB to find data regarding the movement or velocity of the player.
//public Rigidbody PlayerRB;
private float moveHorizontalCamera;
private Vector3 offset;
// Use this for initialization
void Start ()
{ //the offset of the camera to the player
offset = new Vector3(Player.transform.position.x - transform.position.x, transform.position.y - Player.transform.position.y, transform.position.z - Player.transform.position.z);
}
// Update is called once per frame
void Update ()
{
moveHorizontalCamera = Input.GetAxis("Joy X"); //"Joy X" is my right joystick moving left, none, or right resulting in -1, 0, or 1 respectively
//Copied this part from the web, so I half understand what it does.
offset = Quaternion.AngleAxis(moveHorizontalCamera, Vector3.up) * offset;
transform.position = Player.transform.position + offset;
transform.LookAt(Player.transform.position);
}
}
任何帮助都会很棒!
答案 0 :(得分:0)
我建议您使用Vector3.RotateTowards
(https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html)
将当前从玩家指向的矢量转移到摄像头(transform.position - Player.transform.position
)并将其旋转到-Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance
(向量指向玩家速度的相反方向,其大小与所需的大小相对应相机与播放器的距离)。然后,您可以使用Vector3.RotateTowards
通过相应地设置相机位置来围绕播放器旋转相机(平滑或立即)。
类似的东西:
transform.position = Player.transform.position + Vector3.RotateTowards(transform.position - Player.transform.position, -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance, step, .0f);
其中步骤是否以弧度更新角度。 (注意:你应该避免每次更新都调用GetComponent<Rigidbody>()
。最好将它存储在某个地方)
我希望我能正确理解你的问题,这会有所帮助。