- Home /
Timer & Isgrounded Issue (Getkey not working)
Goal is to have a "Hold Space to jump higher" that cuts out after the timer runs out & can only be initiated if the player was Grounded to begin with.
2 problems I'm having.
When using this Timer script with GetKey (constant Addforce no longer works even if I remove the IsGrounded & the Timer)
I can't figure out how to incorporate Is IsGrounded into the timer
(I would alse prefer to incorporate a reflect.Addforce with a contact check but I have not got a script even close to working yet)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float jumpForce;
public float TIMER = 0f;
public float JumpCoolDown = 4f;
bool IsGrounded;
void Start ()
{
rb = GetComponent<Rigidbody> ();
IsGrounded = true;
}
void FixedUpdate ()
{
if (TIMER >= 0f) {
TIMER -= Time.deltaTime;
}
if (TIMER < 0f) {
if (Input.GetKey (KeyCode.Space) && IsGrounded == true) {
jump ();
Debug.Log ("Space working");
}
}
}
void jump ()
{
rb.AddForce(Vector3.up * jumpForce);
TIMER = JumpCoolDown;
}
void OnCollisionExit ()
{
IsGrounded = false;
}
void OnCollisionStay (Collision other)
{
if (other.gameObject.CompareTag ("Ground"))
{
Isgrounded= true;
}
}
}
Your answer
Follow this Question
Related Questions
Should we use Time.deltaTime in FixedUpdate 5 Answers
How to add forward velocity to a Rigidbody2D? 2 Answers
About Forces and Time.deltaTime 3 Answers
How to make my bullets spread when fired 3 Answers