- Home /
Question by
Goldiedog123 · Jul 11, 2017 at 03:43 AM ·
charactercontrollertimeifgrounded
if (controller.isGrounded == false) (for more than) public int time = 1.0F; // do stuff
Hello Unity community ;) your looking nice today.
Soooo for the question: How would I detect if a player isn't grounded for more than a specific time? E.G.
void Jump()
{
if (controller.isGrounded == false && has been false > than time)
{
//stuff
}
Thanks in advance!!
Comment
Answer by Jwizard93 · Jul 11, 2017 at 04:20 AM
public float timeSinceLeftGround;
public float timer;
void Start()
{
timer = 2f; // timer lasts for 2 seconds
}
void Jump()
{
if (!isGrunded && Time.time - timeSInceLeftGround > timer)
{
//do stuff
}
}
void Update()
{
if(!isGrounded)
{
timeSinceLeftGround += time.deltaTime;
}
}
// add this wherever isGrounded is set to false
timeSinceLeftGround = 0f;
Answer by Goldiedog123 · Jul 17, 2017 at 12:18 AM
I solved it with the following
These are the top bits:
public float jumping = 12.0F;
public int maxjumps = 2;
int jumps;
void Jump()
{
if (jumps > 0)
{
CharacterController controller = GetComponent<CharacterController>();
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumping;
jumps = jumps - 1;
}
}
if (jumps == 0)
{
return;
}
}
void JumpReset()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded == true)
{
jumps = maxjumps;
}
}
BTW just play the functions in the update with:
Jump();
JumpReset();
Your answer
Follow this Question
Related Questions
C# get-time code 3 Answers
checking if another object is grounded 1 Answer
character controller grounded 1 Answer