C# 如何在运行时使用新的输入系统在Unity中切换动作贴图?

C# 如何在运行时使用新的输入系统在Unity中切换动作贴图?,c#,unity3d,C#,Unity3d,我现在有一个设置,玩家可以使用WASD向前、向左、向右和向后移动。基本的东西 public class Player : MonoBehaviour { public InputMaster controls; private float playerSpeed = 4f; { controls = new InputMaster(); controls.PlayerN.Movement.performed += ctx => Mov

我现在有一个设置,玩家可以使用WASD向前、向左、向右和向后移动。基本的东西

public class Player : MonoBehaviour
{
    public InputMaster controls;
    private float playerSpeed = 4f;
    {
        controls = new InputMaster();
        controls.PlayerN.Movement.performed += ctx => Movement(ctx.ReadValue<Vector2>());
        controls.PlayerN.Movement.performed += ctx => move = ctx.ReadValue<Vector2>();
        controls.PlayerN.Movement.canceled += ctx => move = Vector2.zero;
    }

    void Update()
    {

        Vector2 inputVector = controls.PlayerN.Movement.ReadValue<Vector2>();
        inputVector = new Vector2(move.x, move.y) * Time.deltaTime * playerSpeed;

        Vector3 finalVector = new Vector3();
        finalVector.x = inputVector.x;
        finalVector.z = inputVector.y;
        transform.Translate(finalVector, Space.World);

    private void OnEnable()
       {
        controls.Enable();
       }

       private void OnDisable()
       {
        controls.Disable();
       }

    }
}

不管怎么说,我目前正被这个问题困扰着,所以如果有人知道如何解决这个问题,我将不胜感激。

好的,我将发布一个代码,按照您在评论中的要求计算3D空间中的玩家移动

首先,假设我们在同一个坐标系中: X-左(-X是右) Y-向上(-Y=向下) Z-向前(-Z=向后)。 然后您需要有3个字段(类级别的变量):

然后在更新方法中执行以下操作:

void Update()
    {
        // You need to where the Player should be moved, like that:
        // Vector2 inputVector = controls.PlayerN.Movement.ReadValue<Vector2>();
        // But I do not know what did you have there, so usually I read them from
        // AWSD keys:
        float x = 0, z = 0;
        var pressedKey = Keyboard.GetPressedKey(); // depends on your platform
        if (pressedKey == "W") // move forward
        {
              z = 1;
        }
        if (pressedKey == "A") // move left
        {
              x = 1;
        }
        // and for remaining keys similar code, but assign -1 accordingly.  
        var inputVectorForMovement = new Vector3(x, 0, z);

        // Then you have to get rotation angle, again I do not know how you do it, 
        // but it is usually obtained from mouse movement:
        var currentMousePosition = Mouse.GetPosition(); // depends on your platform
        // I just did simple approximation here to rotate on the angle of 45 degrees converted to radians per 100 pixels of mouse movement:
        var horizontalRotationAngleRadians = Math.Pi/4f * (currentMousePosition.X - _lastMousePosition.X) / 100f;
        
         
        // Create rotation matrix assuming Y is up vector: 
        var rotationMatrixForPlayerDirection = Matrix.CreateRotationY(horizontalRotationAngleRadians);
       
        // Calculate player direction:
        var playerDirection = _playerLooksAt - _playerPosition;

        // Normalize it*, so the direction vector will have coefficients per each axis indicating how much the position should be moved to either side:
        playerDirection.Normalize();
        
        // and rotate it:
        var rotatedPlayerDirection = Vector3.Transfom(playerDirection, rotationMatrixForPlayerDirection);
        
        // Then you can simply move forward or backward along this vector:
        var movementOnVector = (rotatedPlayerDirection * Time.deltaTime * playerSpeed * inputVectorForMovement.Z);
        _position += movementOnVector;

        // To move left or right according to the direction vector 
        // you have to calculate a vector perpendicular 
        // to both of direction vector and up vector (which is Y)**:
        var sideDirectionVector = Vector3.Cross(rotatedPlayerDirection, Vector3.UnitY);
        // and just do the movement along that vector:
        movementOnVector = (sideDirectionVector * Time.deltaTime * playerSpeed * inputVectorForMovement.X);
        _position += movementOnVector;

        // Do not forget to update fields:
        _playerLooksAt = _position + rotatedPlayerDirection;
        _lastMousePosition = currentMousePosition;
}
void Update()
{
//你需要知道玩家应该被移动到哪里,比如:
//Vector2 inputVector=controls.PlayerN.Movement.ReadValue();
//但我不知道你那里有什么,所以我通常是从
//AWSD键:
浮动x=0,z=0;
var pressedKey=Keyboard.GetPressedKey();//取决于您的平台
如果(按键==“W”)//向前移动
{
z=1;
}
如果(按按键==“A”)//向左移动
{
x=1;
}
//对于其余的键,使用类似的代码,但相应地分配-1。
var inputVectorForMovement=新矢量3(x,0,z);
//然后你必须得到旋转角度,我也不知道你是怎么做到的,
//但通常通过鼠标移动获得:
var currentMousePosition=Mouse.GetPosition();//取决于您的平台
//我只是在这里做了简单的近似,以45度的角度旋转,转化为每100像素鼠标移动的弧度:
var horizontalRotationAngleRadians=Math.Pi/4f*(currentMousePosition.X-_lastMousePosition.X)/100f;
//假设Y是上方向向量,则创建旋转矩阵:
var RotationMatrix FormlayerDirection=矩阵.CreateRotationY(水平旋转角度弧度);
//计算玩家方向:
变量playerDirection=\u playerLooksAt-\u playerPosition;
//对其进行规格化*,使方向向量具有每个轴的系数,指示位置应移动到任一侧的程度:
playerDirection.Normalize();
//并将其旋转:
var rotatedplayerddirection=Vector3.transform(playerddirection、rotationmatrix或layerddirection);
//然后您可以简单地沿着该向量向前或向后移动:
var movementOnVector=(rotatedPlayerDirection*Time.deltaTime*playerSpeed*inputVectorForMovement.Z);
_位置+=移动向量;
//根据方向向量向左或向右移动
//你必须计算一个垂直的向量
//方向向量和上方向向量(Y)**:
var sideDirectionVector=Vector3.Cross(旋转播放方向,Vector3.UnitY);
//沿着这个向量做运动:
movementOnVector=(sideDirectionVector*Time.deltaTime*playerSpeed*inputVectorForMovement.X);
_位置+=移动向量;
//不要忘记更新字段:
_playerLooksAt=_位置+旋转的PlayerDirection;
_lastMousePosition=currentMousePosition;
}
*


**

你为什么把事情弄得这么复杂?我更喜欢用一个动作来移动,只需旋转运动方向向量。简短回答?因为我是noob lol。我会尝试一下,然后报告结果。嗯……你是怎么做到的?我正在努力寻找改变方向向量的位置和方式:(
//Actual position of the player in 3d space
Vector3 _playerPosition;

// The point in 3d space at which the player is looking right now
// If he can move only horizontally - the Y coordinate can be locked.
Vector3 _playerLooksAt;

// Last known mouse position
Vector2 _lastMousePosition;
void Update()
    {
        // You need to where the Player should be moved, like that:
        // Vector2 inputVector = controls.PlayerN.Movement.ReadValue<Vector2>();
        // But I do not know what did you have there, so usually I read them from
        // AWSD keys:
        float x = 0, z = 0;
        var pressedKey = Keyboard.GetPressedKey(); // depends on your platform
        if (pressedKey == "W") // move forward
        {
              z = 1;
        }
        if (pressedKey == "A") // move left
        {
              x = 1;
        }
        // and for remaining keys similar code, but assign -1 accordingly.  
        var inputVectorForMovement = new Vector3(x, 0, z);

        // Then you have to get rotation angle, again I do not know how you do it, 
        // but it is usually obtained from mouse movement:
        var currentMousePosition = Mouse.GetPosition(); // depends on your platform
        // I just did simple approximation here to rotate on the angle of 45 degrees converted to radians per 100 pixels of mouse movement:
        var horizontalRotationAngleRadians = Math.Pi/4f * (currentMousePosition.X - _lastMousePosition.X) / 100f;
        
         
        // Create rotation matrix assuming Y is up vector: 
        var rotationMatrixForPlayerDirection = Matrix.CreateRotationY(horizontalRotationAngleRadians);
       
        // Calculate player direction:
        var playerDirection = _playerLooksAt - _playerPosition;

        // Normalize it*, so the direction vector will have coefficients per each axis indicating how much the position should be moved to either side:
        playerDirection.Normalize();
        
        // and rotate it:
        var rotatedPlayerDirection = Vector3.Transfom(playerDirection, rotationMatrixForPlayerDirection);
        
        // Then you can simply move forward or backward along this vector:
        var movementOnVector = (rotatedPlayerDirection * Time.deltaTime * playerSpeed * inputVectorForMovement.Z);
        _position += movementOnVector;

        // To move left or right according to the direction vector 
        // you have to calculate a vector perpendicular 
        // to both of direction vector and up vector (which is Y)**:
        var sideDirectionVector = Vector3.Cross(rotatedPlayerDirection, Vector3.UnitY);
        // and just do the movement along that vector:
        movementOnVector = (sideDirectionVector * Time.deltaTime * playerSpeed * inputVectorForMovement.X);
        _position += movementOnVector;

        // Do not forget to update fields:
        _playerLooksAt = _position + rotatedPlayerDirection;
        _lastMousePosition = currentMousePosition;
}