使用刚体+新输入系统+电影机的第三人称相机移动



到目前为止,我有一个使用刚体和新输入系统的工作运动系统。我把它设置成WASD通过输入,然后向前、向后、向左和向右移动角色,同时在按下时也面向那个方向。

我还有一个FreeLook电影机相机,它可以在玩家移动时跟踪他们,目前效果很好,可以在玩家周围移动。

此外,我希望添加功能;向前";并且通过扩展,其他移动选项与相机所面对的方向相关。因此,如果你在玩家面前移动相机,那么";向前";现在会是相反的方向,等等。这是我一直坚持的部分,因为我不知道如何将我的输入从向前改变为相对于相机向前。我应该只是相对于相机旋转整个游戏对象吗?但我只希望角色在尝试移动时旋转。

tl;drDMC5运动系统,如果它更容易显示而不显示的话。以下是我目前所拥有的:

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

}

}

根据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;  

(当然,对playercam的引用旨在作为伪代码,用适当的引用替换它们(

相关内容

最新更新