我試圖讓這個角色在空中停止跳躍,但問題是當它與牆壁碰撞時會跳躍。我怎樣才能阻止雙跳的人物?
因此,我試圖讓一定的時間通過之前,字符可以在下面的腳本再次跳轉,但由於某種原因,它不起作用。
如果有人有任何更有效的方式來做到這一點,我一定會很感激。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float UpSpeed;
public Rigidbody rb;
public float horizonSpeed;
public bool isGrounded;
public float gravInc = 50;
public int counter;
public float DownSpeed = 50;
public int jumpCount = 0;
public float secCount = 0;
public bool validUp = true;
public float sec;
public void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
public void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
public void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (secCount == (sec))
{
validUp = true;
}
sec = Time.time;
if (Input.GetKeyDown ("up") && isGrounded == (true) && validUp == (true))
{
rb.AddForce(transform.up * UpSpeed);
secCount = 0;
validUp = false;
secCount = sec + 3;
}
else if(Input.GetKeyDown ("right"))
{
horizonSpeed = 200;
rb.AddForce(transform.right * horizonSpeed);
}
else if (Input.GetKeyDown ("left"))
{
horizonSpeed = -200;
rb.AddForce(transform.right * horizonSpeed);
}
}
}
謝謝,我試過了,它工作。 – user8077453
如果有效,請使用答案左邊的複選標記來接受它! – ashbygeek