C#unity Double Jump Coding for player Movement control

时间:2013-09-25 10:36:36

标签: c# unity3d dynamic

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
public float speed          = 3.0f;
public float jumpSpeed          = 200.0f;
public bool grounded            = true;
public float time           = 4.0f;     



// Use this for initialization
void Start () 
{

}

// Update is called once per frame
void FixedUpdate () 
{
    Vector3 x = Input.GetAxis("Horizontal")* transform.right * Time.deltaTime *      speed;

    if (time <= 2)
    {
    if(Input.GetButtonDown("Jump"))
        {
                Jump();
        }

    }

    transform.Translate(x);

    //Restrict Rotation upon jumping of player object
    transform.rotation = Quaternion.LookRotation(Vector3.forward);


}
void Jump()
    {
        if (grounded == true)
        {
            rigidbody.AddForce(Vector3.up* jumpSpeed);


            grounded = false;
        }

    }
void OnCollisionEnter (Collision hit)
{
    grounded = true;
    // check message upon collition for functionality working of code.
    Debug.Log ("I am colliding with something");
}

}

应该在哪里以及什么类型的编码可以让它在回到地面之前跳两次?

有一个带有精灵表的对象,我已经获得了基于物理引擎统一约束运动和正常跳跃。但是我希望机芯更加动态,只有在没有接地的情况下跳转两次,并且在某个时间范围之间跳跃,就像跳跃按钮在一些Milli秒间隔内被按下,然后在地面上休息时重置位置。

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

private bool dblJump = true;

void Jump()
{
    if (grounded == true)
    {
        rigidbody.AddForce(Vector3.up* jumpSpeed);

        grounded = false;
    } 
    else if (!grounded && dblJump)
    {
        rigidbody.AddForce(Vector3.up* jumpSpeed);

        dblJump = false;
    }

}

void OnCollisionEnter (Collision hit)
{
    grounded = true;
    dblJump = true;
    // check message upon collition for functionality working of code.
    Debug.Log ("I am colliding with something");
}