C#统一双跳编码,用于玩家移动控制

本文关键字:编码 用于 玩家 控制 移动 一双 | 更新日期: 2023-09-27 18:32:40

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");
}
}

应该在哪里以及哪种类型的编码可以使它在返回地面之前跳两次?

上面

有一个带有精灵表的对象,我已经在统一中获得了基于物理引擎的运动约束和正常跳跃。 但我希望运动更加动态,并且仅在未接地且在特定时间范围内进行两次跳跃,例如如果跳跃按钮在静止在地面上时在某个毫秒间隔内按下,然后重置位置。

C#统一双跳编码,用于玩家移动控制

这应该可以解决问题:

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");
}