统一跳跃技工不工作

Unity Jumping Mechanic not working

我有一个 3D 游戏 space,还有刚体合成。给玩家。我使用以下代码实现了一个无法正常工作的跳跃机制:

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坐标。除非有人有楼层检测的替代)。

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

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

嗨,对于那些登陆这里寻找更多关于他们的刚体行为不符合他们预期方式的信息的人,我从 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;
    }