【问题标题】:Use Mobile Joystick with Unity 3d RigidBody将移动操纵杆与 Unity 3d RigidBody 配合使用
【发布时间】:2021-10-17 00:22:21
【问题描述】:

我正在开发一个移动 3D 应用程序。我有一个游戏对象,它有一个刚体,我用操纵杆移动它。

我做到了,当我接受垂直输入时,我可以向前移动对象,当我从操纵杆接受水平输入时,我可以旋转它。

问题是,当我将刚体旋转 90 度时,我希望使用水平输入来向前或向后移动对象,并使用垂直操纵杆输入来旋转它。基本上我希望对象在操纵杆被拉动的地方向前移动,但是在当前设置下,即使对象面向左侧或右侧,我也必须向上拉操纵杆。我希望我解释得很好。如果不知道,请告诉我,我会详细解释。

    public Joystick m_Joystick;
    private float m_HorizontalValue;
    private float m_VerticalValue;

    void Update()
    {
        m_VerticalValue = m_Joystick.Vertical;
        m_HorizontalValue = m_Joystick.Horizontal;
    }

    // Same as update, but is used fo modifying values that are used to manipulate
    // game object's physics
    void FixedUpdate() 
    {
        Move();
        Turn();
    }

    void Move()
    {
        Vector3 movement = transform.forward * m_VerticalValue * m_Speed * Time.deltaTime;

        m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
    }

    void Turn()
    {
        float turn = m_HorizontalValue * m_TurnSpeed * Time.deltaTime;

        Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);

        m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation); 
    }

我尝试检查刚体的前向矢量(x 和 z 值)以在将操纵杆拉到所需方向时反转对象的运动,但它不能正常工作,因为当 x 和 z 时刚体会卡住前向向量的值相等。代码不完整我只测试了 x 和 z 坐标系的两个四分之一(第一和第二个四分之一)。

    float GetVerticalOrHorizaontalValueMovement()
    {
        float xValue = m_Rigidbody.transform.forward.x;
        float zValue = m_Rigidbody.transform.forward.z;

        float auxX = Mathf.Abs(xValue);
        float auxZ = Mathf.Abs(zValue);

        if (xValue > -0.1f && zValue > -0.1f)
        {
            if (auxX <= auxZ)
                return  m_HorizontalValue;
            else if (auxX >= auxZ)
                return m_VerticalValue;
        }
        else if(xValue > -0.1f && zValue < 0.1f)
        {
            if (auxX >= auxZ)
                return m_VerticalValue;
            else if (auxX <= auxZ)
                return m_HorizontalValue;
        }

        return 0f;
    }

    float GetVerticalOrHorizaontalValueRotation()
    {
        float xValue = m_Rigidbody.transform.forward.x;
        float zValue = m_Rigidbody.transform.forward.z;

        float auxX = Mathf.Abs(xValue);
        float auxZ = Mathf.Abs(zValue);

        if (xValue > -0.1f && zValue > -0.1f)
        {
            if(auxX <= auxZ)
                return m_VerticalValue;
            else if(auxX >= auxZ)
                return m_HorizontalValue;
        }
        else if(xValue > -0.1f && zValue < 0.1f)
        {
            if (auxX >= auxZ)
                return m_HorizontalValue;
            else if (auxX <= auxZ)
                return m_VerticalValue;
        }

        return 0f;
    }

这些方法将被放置在 Move() 和 Turn() 方法中,而不是 m_VerticalValue 和 m_Horizo​​ntalValue 变量。

我对任何可以让我的游戏对象使用操纵杆随心所欲地移动的解决方案持开放态度,但我必须将其保留为 RigidBody,因为我想在它和其他可能的强制器上施加爆炸力。如果我没有正确解释某些事情,请告诉我。

【问题讨论】:

    标签: c# unity3d rigid-bodies


    【解决方案1】:

    如果有人需要它。这里是。使用移动操纵杆进行简单的移动。如果您有复杂的动作,您可能需要根据需要对其进行修改

     using UnityEngine;
     
     public class MovementOfPlayer : MonoBehaviour
     {
         Vector3 movementInput;
         Rigidbody playerRigidbody;
     
         void Start()
         {
             playerRigidbody = GetComponent<Rigidbody>();
             playerRigidbody.freezeRotation = true;
         }
     
         void FixedUpdate()
         {
             movementInput = Input.GetAxisRaw("Horizontal") * Vector3.right +
                             Input.GetAxisRaw("Vertical") * Vector3.forward;
             movementInput.Normalize();
     
             float y = playerRigidbody.velocity.y;
     
             if (movementInput != Vector3.zero)
             {
                 if (transform.forward != movementInput)
                 {
                     transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(movementInput), Time.deltaTime * 180);
     
                     playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, Vector3.zero, Time.deltaTime * 30);
                 }
                 else
                 {
                     playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, movementInput * 10, Time.deltaTime * 30);
                 }
             }
             else
             {
                 playerRigidbody.velocity = Vector3.MoveTowards(playerRigidbody.velocity, Vector3.zero, Time.deltaTime * 30);
             }
             Vector3 velocity = playerRigidbody.velocity;
             velocity.y = y;
             playerRigidbody.velocity = velocity;
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多