Unity3D双跳

时间:2019-06-01 19:07:15

标签: unity3d

我正在尝试使自己的球面跳两次,但似乎有些毛病且迟了,有时跳得比我想要的多或少。我曾尝试观看视频和问题,但它们大多用于2D。

我曾尝试更改条件和值,但要么跳得不如我想的那么快,要么跳得太晚了。

using System.Collections;

using System.Collections.Generic; 使用UnityEngine;

公共类ControllerScript:MonoBehaviour {

public float speed;
public float jumpVelocity;
public float fallMultiplier = 2.5f;
public int currentJump = 0;

private bool isGrounded;


private Rigidbody rb;


void Start()
{
    rb = GetComponent<Rigidbody>();
}

void Update()
{
    if(isGrounded == true)
    {
        currentJump = 2;
    }


}


void FixedUpdate()
{


    float moveHorizontal = Input.GetAxisRaw("Horizontal");
    float moveVertical = Input.GetAxisRaw("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    movement = Vector3.ClampMagnitude(movement, 1);

    rb.AddForce(movement * speed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.Space) && currentJump > 0)
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jumpVelocity;
        currentJump--;
    }
    else if(Input.GetKeyDown(KeyCode.Space) && currentJump == 0 && isGrounded == true)
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jumpVelocity;
    }

    if (rb.velocity.y < 0)
    {
        rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    }


}
void OnCollisionEnter(Collision collision)
{
    Debug.Log("Entered");
    if (collision.gameObject.CompareTag("Platform"))
    {
        isGrounded = true;

    }

}

void OnCollisionExit(Collision collision)
{
    Debug.Log("Exited");
    if (collision.gameObject.CompareTag("Platform"))
    {
        isGrounded = false;

    }

}

}

我希望它能跳两次。

0 个答案:

没有答案