如何解决使用刚体 + 新输入系统 + Cinemachine 的第三人称摄像机运动
到目前为止,我有一个使用 Rigidbody 和新输入系统的工作移动系统。我已将其设置为 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,_playerDirection.y);
transform.LookAt(transform.position + new Vector3(inputVector.x,inputVector.z));
_rb.veLocity = inputVector * _speed;
}
private void OnMoveButton(Vector2 direction)
{
if (canMove)
{
_playerDirection = direction;
}
}
}
解决方法
根据这个post on Unity Forum,你可以使用player.transfrom.eulerAngles
代替LookAt
函数。
这可能是您重写 FixedUpdate
的方式,假设您想使用相机的角度围绕 Y 轴旋转您的角色:
Vector3 inputVector = new Vector3(_playerDirection.x,_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
的引用是伪代码,用适当的引用替换它们)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。