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