Unity3d 使用刚体的第三人称摄影机移动&x2B;新的输入系统&x2B;电影机

Unity3d 使用刚体的第三人称摄影机移动&x2B;新的输入系统&x2B;电影机,unity3d,input,cinemachine,Unity3d,Input,Cinemachine,到目前为止,我有一个工作的运动系统使用刚体和新的输入系统。我已经设置好了WASD,这样WASD就可以通过输入,然后在按下时将字符向前、向后、向左和向右移动,同时也面向该方向 我还有一个FreeLook Cinemachine摄像头,它可以随着玩家的移动而移动,目前效果很好,可以在玩家周围移动 此外,我想添加功能,以便“前进”和其他移动选项与相机所面对的方向相关联。因此,如果你在播放器前面移动相机,那么“向前”将是相反的方向,依此类推。这是我被卡住的部分,因为我不确定如何将输入从前进更改为相对于相

到目前为止,我有一个工作的运动系统使用刚体和新的输入系统。我已经设置好了WASD,这样WASD就可以通过输入,然后在按下时将字符向前、向后、向左和向右移动,同时也面向该方向

我还有一个FreeLook Cinemachine摄像头,它可以随着玩家的移动而移动,目前效果很好,可以在玩家周围移动

此外,我想添加功能,以便“前进”和其他移动选项与相机所面对的方向相关联。因此,如果你在播放器前面移动相机,那么“向前”将是相反的方向,依此类推。这是我被卡住的部分,因为我不确定如何将输入从前进更改为相对于相机的前进。我是否应该相对于相机旋转整个游戏对象?但我只希望角色在尝试移动时旋转

tl;dr DMC5运动系统,如果它更容易显示和不告诉。 以下是我到目前为止的情况:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerCharacterController : MonoBehaviour
 {
     private PlayerActionControls _playerActionControls;
 
     [SerializeField]
     private bool canMove;
     [SerializeField]
     private float _speed;
     private Vector2 _playerDirection;
     private GameObject _player;
     private Rigidbody _rb;
     private Animator _animator;
     
 
     private void Awake()
     {
         _playerActionControls = new PlayerActionControls();
         _player = this.gameObject;
         _rb = _player.GetComponent<Rigidbody>();           
     }
 
     private void OnEnable()
     {
         _playerActionControls.Enable();
         _playerActionControls.Default.Move.performed += ctx => OnMoveButton(ctx.ReadValue<Vector2>());
         
     }
 
     private void OnDisable()
     {
         _playerActionControls.Default.Move.performed -= ctx => OnMoveButton(ctx.ReadValue<Vector2>());        
         _playerActionControls.Disable();
     }   
 
     private void FixedUpdate()
     {            
          Vector3 inputVector = new Vector3(_playerDirection.x, 0, _playerDirection.y);        
          transform.LookAt(transform.position + new Vector3(inputVector.x, 0, inputVector.z));
         _rb.velocity = inputVector * _speed;               
     }
 
     private void OnMoveButton(Vector2 direction)
     {
         if (canMove)
         {
             _playerDirection = direction;
         }             
             
     }
 
 }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.InputSystem;
公共类PlayerCharacterController:单行为
{
专用PlayerActionControls_PlayerActionControls;
[序列化字段]
私人布尔可以移动;
[序列化字段]
私人浮动速度;
专用矢量2_播放器方向;
私人游戏对象(player);;
私人刚体;
私人动画师;
私人空间
{
_playerActionControls=新的playerActionControls();
_player=this.gameObject;
_rb=_player.GetComponent();
}
私有void OnEnable()
{
_playerActionControls.Enable();
_playerActionControls.Default.Move.performed+=ctx=>OnMoveButton(ctx.ReadValue());
}
私有无效不可撤销()
{
_playerActionControls.Default.Move.performed-=ctx=>OnMoveButton(ctx.ReadValue());
_playerActionControls.Disable();
}   
私有void FixedUpdate()
{            
Vector3 inputVector=新的Vector3(_playerDirection.x,0,_playerDirection.y);
transform.LookAt(transform.position+newvector3(inputVector.x,0,inputVector.z));
_rb.velocity=输入向量*\u速度;
}
移动按钮上的专用void(矢量2方向)
{
如果(可以移动)
{
_playerDirection=方向;
}             
}
}
根据这一点,您可以使用
player.transfrom.eulerAngles
而不是
LookAt
功能

这可能是重写
FixedUpdate
的方法,假设您希望使用相机角度绕Y轴旋转角色:

Vector3 inputVector = new Vector3(_playerDirection.x, 0, _playerDirection.y); 

// rotate the player to the direction the camera's forward
player.transform.eulerAngles = new Vector3(player.transform.eulerAngles.x,
 cam.transform.eulerAngles.y, player.transform.eulerAngles.z);

// "translate" the input vector to player coordinates
inputVector = player.transform.TransformDirection(inputVector);

_rb.velocity = inputVector * _speed;  

(当然,对
player
cam
的引用是作为伪代码,用适当的引用替换它们)

感谢您的回复。我最终尝试了很多东西,虽然它确实正确地旋转了玩家,但向量仍然是全局的,所以W相对于游戏世界仍然是向前的,而不是相机面对的位置,我想我需要一种方法来把通过输入系统传递的向量转换成相对于摄像机和播放器的局部向量,但是我不知道如何。我编辑了原始答案,提醒你,你仍然通过<代码>输入向量*速度<代码>作为刚体的速度,它不考虑玩家旋转。试试新编辑的答案。我回家后会试试这个。谢谢:)让我知道这对你是否有效。那很好。我没有完全理解问题的所有方面。问题在于,移动是通过刚体速度沿
播放器.forward
的方向应用的,并且只检查了输入向量的大小,以便在不按下移动轴时播放器不会移动。检查最新更新的答案。