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

时间:2020-11-02 10:49:42

标签: c# unity3d

我目前有一个设置,播放器可以使用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();
       }

    }
}

以上是我如何称呼运动。您可能会注意到该动作图被称为PlayerN,这是因为我总共有4个动作图,并且我的想法是当相机围绕播放器旋转90度时,我希望播放器切换到下一个动作图(北,东,南,西)。例如,我的动作图“ PlayerE”在W上向左移动,在S上向右移动,在A上向下移动,在D上向上移动。我只是重新调整了WASD键的方向。

我现在对于如何切换到其他动作图一无所知。我查看了新输入系统的文档,但找不到任何好的信息。

我对如何解决此问题有一个想法,但是我不知道我是否走在正确的轨道上。我有一个switch循环,用于检查另一个名为camDirection的脚本中的int,并且我的void Update()函数中有这部分。

 switch (refScript.camDirection)
        {
            case -3:
                Debug.Log("Facing East");
                break;
            case -2:
                Debug.Log("Facing South");
                break;
            case -1:
                Debug.Log("Facing West");
                break;
            case 0:
                Debug.Log("Facing North");
                break;
            case 1:
                Debug.Log("Facing East");
                break;
            case 2:
                Debug.Log("Facing South");
                break;
            case 3:
                Debug.Log("Facing West");
                break;
        }

无论如何,我目前对此仍然感到困惑,因此,如果有人对如何解决此问题有任何想法,我将非常感激。

1 个答案:

答案 0 :(得分:1)

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

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

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

然后在您的Update方法中执行以下操作:

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

* normalize vector

** cross product