- Home /
My Character controller keeps jumping infinitely
I have followed an old unfinished tutorial to try and learn C# and I got the character controller working but when i jump my character controller infinitely jumping. i have tried multiple things but nothing has worked. i'm not asking for people to do it for me, i want an explanation on how to make it work properly.
thanks in advance.
here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMov : MonoBehaviour {
public Transform playertrn;
public Rigidbody playerRB;
public int speed;
private bool isFalling = false;
private bool doubleJump = false;
public Vector3 JumpHeight = new Vector3 (0, 8, 0);
void Start ()
{
playertrn = GetComponent<Transform> ();
playerRB = GetComponent<Rigidbody> ();
Physics.gravity = new Vector3(0, -15.0F, 0);
}
void Update ()
{
if (Input.GetKey (KeyCode.D))
{
playertrn.Translate (new Vector3(speed, 0, 0) * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{
playertrn.Translate (new Vector3 (-speed, 0, 0) * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.Space))
{
playerRB.velocity = JumpHeight;
isFalling = true;
}
} void OnCollisionStay () { isFalling = false; } }
Answer by NorthStar79 · Jul 19, 2017 at 06:30 AM
if (Input.GetKey (KeyCode.Space) && !isFalling)
{
playerRB.velocity = JumpHeight;
isFalling = true;
}
Change this part like this.
Thank you so much this has done exactly what i wanted it to
Answer by ZeN12 · Jul 18, 2017 at 07:44 AM
Change Input.GetKey to Input.GetKeyUp
yeah i did but if i keep pressing space it still jumps infinitely.
I think the problem is with logic, you need to check if the player is "grounded" (touches the ground). Here is a tutorial for this: https://www.youtube.com/watch?v=6IJza-xEP$$anonymous$$s
Your answer
Follow this Question
Related Questions
Jump while running not faster than normal jump. Any help with the code? 1 Answer
Shift BHOP in Character Controller 0 Answers
[SOLVED] 2D Character Controller gains velocity when colliding with a corner 1 Answer
how to make 2d character jump? 0 Answers
,How to disable autojumping when holding spacebar? 4 Answers