3

(对于 Unity 5.3.5f1)现在我正在制作一个 3D 摄像机,它围绕玩家水平旋转。玩家是一个刚体滚动球体。玩家可以自由水平旋转轴,但是当没有输入时,我希望旋转重置回速度方向。

现在我只需要知道如何通过从之前的旋转围绕玩家旋转相机来将相机定位在玩家速度方向的后面。

这是我正在寻找的粗略图: 在此处输入图像描述

目前要更新相机以围绕播放器运行,我在相机上使用此脚本(带注释):

 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);
     }
 }

任何帮助都会很棒!

4

1 回答 1

0

我建议您使用Vector3.RotateTowardshttps://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html

您将当前从 Player 指向相机的向量 ( 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);

如果角度以弧度更新,则在哪里 step 。(注意:你应该避免调用GetComponent<Rigidbody>()每个更新。你最好将它存储在某个地方)

我希望我正确理解了您的问题,这会有所帮助。

于 2016-06-08T17:11:20.523 回答