Question by
suryono021 · Jun 09, 2020 at 08:56 PM ·
jumpjumpingdelayjumping objectgetkey
How to put delay after input key
i very confuse, i want to put some delay after character jump, i saw from google and other, tried it, but it get eror, really confuse bout it, please explain me how to put delay on getkey, thanks, sorry for my bad english
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb; //rb = rigidbody
public float ForwardForce = 2000f;// tekanan pas maju
public float SidewaysForce = 500f; // kasih input buat gerak kesamping
public float CharacterRam = 300f;
public float JumpForce = 1000f;
public float timeBetweenInputKey = 0.1111f; // kasi delay pake 1 key, 1 detik setiap input
private float timestamp;
// Update is called once per frame
void Update () {
// menambah tekanan maju
rb.AddForce (0,0, ForwardForce * Time.deltaTime); // add a force of 2000 on z-Axis
if (Input.GetKey ("d")) {
rb.AddForce (SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
} else if (Input.GetKey ("a")) {
rb.AddForce (-SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
} else if (rb.position.y < -1f) {
FindObjectOfType<GameManager> ().EndGame ();
} else if (Input.GetKey ("q")) {
rb.AddForce (0, CharacterRam * Time.deltaTime, 0, ForceMode.VelocityChange);
} else (Time.time >= timestamp && (Input.GetKey ("r"))){
rb.AddForce (0, JumpForce * Time.deltaTime,0 , ForceMode.VelocityChange);
timestamp = Time.time + timeBetweenInputKey;
}
}
}
Comment