Unity Jumping Mechanic无法正常工作

时间:2018-03-13 00:58:24

标签: c# unity3d game-physics

我在3D空间玩游戏,而刚体comp。对一个球员。我已经使用以下代码实现了一个无法正常工作的跳转机制:

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

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