Exemplo n.º 1
0
    private Vector3 CalculateMovementDirection()
    {
        //If no character input script is attached to this object, return no input;
        if (characterInput == null)
        {
            return(Vector3.zero);
        }

        Vector3 _direction = Vector3.zero;

        //If no camera transform has been assigned, use the character's transform axes to calculate the movement direction;
        if (cameraTransform == null)
        {
            _direction += tr.right * characterInput.GetHorizontalMovementInput();
            _direction += tr.forward * characterInput.GetVerticalMovementInput();
        }
        else
        {
            //If a camera transform has been assigned, use the assigned transform's axes for movement direction;
            //Project movement direction so movement stays parallel to the ground;
            _direction += Vector3.ProjectOnPlane(cameraTransform.right, tr.up).normalized *characterInput.GetHorizontalMovementInput();
            _direction += Vector3.ProjectOnPlane(cameraTransform.forward, tr.up).normalized *characterInput.GetVerticalMovementInput();
        }

        //If necessary, clamp movement vector to magnitude of 1f;
        if (_direction.magnitude > 1f)
        {
            _direction.Normalize();
        }

        return(_direction);
    }