2

到目前为止,我有一个使用刚体和新输入系统的工作运动系统。我进行了设置,以便 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;
         }             
             
     }
 
 }
4

1 回答 1

1

根据Unity Forum 上的这篇文章,您可以使用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是伪代码,用适当的引用替换它们)

于 2020-12-26T09:58:39.830 回答