【问题标题】:Unity Jumping Mechanic not workingUnity Jumping Mechanic 不工作
【发布时间】:2018-03-13 00:58:23
【问题描述】:

我有一个 3D 空间中的游戏和刚体组合。给玩家。我使用以下代码实现了一个无法按预期工作的跳转机制:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public bool Travel;
    public Rigidbody rb;
    public Transform tf;
    public bool Grounded;
    public float speed;

    int OnRail = 2;

    void Update ()
    {
        float speed = 1f * Time.deltaTime;
        if (Input.GetKeyDown("d") && OnRail < 3)
        {
            tf.Translate(5.4f, 0f, 0f);
            OnRail++;
        }
        else if (Input.GetKeyDown("a") && OnRail > 1)
        {
            tf.Translate(-5.4f, 0f, 0f);
            OnRail--;
        }
    }

    void FixedUpdate ()
    {
        if (Travel)
        {
            rb.velocity = new Vector3(0, 0, speed * Time.deltaTime);
        }

        if (Input.GetKeyDown("space") && Grounded)
        {
            Grounded = false;
            rb.AddForce(new Vector3(0, 500000f * Time.deltaTime, 0));

        }

        if ((tf.position.y == 2f || tf.position.y == 10f) && rb.velocity.y == 0 && !Grounded)
        {
            Grounded = true;
        }
    }
}

玩家应该跳到空中,但只能跳几乎没有距离。 2 和 10 Y 位置是预设的楼层(我真的不需要这 2 个 Y 坐标。除非有人替代了楼层检测)。

【问题讨论】:

  • 您可以参考this Unity Answers 帖子来检查您的播放器是如何接地的。

标签: c# unity3d game-physics


【解决方案1】:

这是因为GetKeyDown 仅针对一帧出现,这意味着您仅对该单帧施加向上的力。此外,Time.deltaTime 是完成最后一帧所需的时间(一个非常小的值),这意味着您向上施加的力非常小。如果您只想在空格键上施加单个力,则无需在此处使用 Time.deltaTime。做吧:

rb.AddForce(Vector3.up * 100f); // or some other reasonable multipliier

【讨论】:

    【解决方案2】:

    您好,对于那些在此上寻找更多关于其刚体行为不符合他们预期方式的信息的人,我从 Unity ForceMode's 中提取了一些相关信息。快乐发展。

        //Here, switching modes depend on button presses in the Game mode
        switch (m_ModeSwitching)
        {
            //This is the starting mode which resets the GameObject
            case ModeSwitching.Start:
                //This resets the GameObject and Rigidbody to their starting positions
                transform.position = m_StartPos;
                m_Rigidbody.transform.position = m_StartForce;
                //This resets the velocity of the Rigidbody
                m_Rigidbody.velocity = new Vector3(0f, 0f, 0f);
                break;
    
            //These are the modes ForceMode can force on a Rigidbody
            //This is Acceleration mode
            case ModeSwitching.Acceleration:
                //The function converts the text fields into floats and updates the Rigidbody’s force
                MakeCustomForce();
                //Use Acceleration as the force on the Rigidbody
                m_Rigidbody.AddForce(m_NewForce, ForceMode.Acceleration);
                break;
    
            //This is Force Mode, using a continuous force on the Rigidbody considering its mass
            case ModeSwitching.Force:
                //Converts the text fields into floats and updates the force applied to the Rigidbody
                MakeCustomForce();
                //Use Force as the force on GameObject’s Rigidbody
                m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
                break;
    
            //This is Impulse Mode, which involves using the Rigidbody’s mass to apply an instant impulse force.
            case ModeSwitching.Impulse:
                //The function converts the text fields into floats and updates the force applied to the Rigidbody
                MakeCustomForce();
                //Use Impulse as the force on GameObject
                m_Rigidbody.AddForce(m_NewForce, ForceMode.Impulse);
                break;
    
    
            //This is VelocityChange which involves ignoring the mass of the GameObject and impacting it with a sudden speed change in a direction
            case ModeSwitching.VelocityChange:
                //Converts the text fields into floats and updates the force applied to the Rigidbody
                MakeCustomForce();
                //Make a Velocity change on the Rigidbody
                m_Rigidbody.AddForce(m_NewForce, ForceMode.VelocityChange);
                break;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2014-12-11
      • 2015-06-14
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多