- Home /
Auto Jump on 3d platform glitch
Hi, I'm a beginner and I'm trying to do a simple autojump, but I got some glitches. Here is the glitch: it jumps some times lot more than it should, sometimes lot less, and always at different altitude. Here is my script: (ofc I have all "using...")
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float JumpForce;
private Rigidbody rig;
void Awake()
{
// get the components
rig = GetComponent<Rigidbody>();
}
void Update()
{
move();
if (Input.GetKey("space"))
{
tryJump();
}
//else if (Input.GetButtonDown("Jump"))
// tryJump();
}
void tryJump()
{
Ray ray1 = new Ray(transform.position + new Vector3 (0.50f,0,0.5f), Vector3.down);
Ray ray2 = new Ray(transform.position + new Vector3(0.5f, 0, -0.5f), Vector3.down);
Ray ray3 = new Ray(transform.position + new Vector3(-0.5f, 0, 0.5f), Vector3.down);
Ray ray4 = new Ray(transform.position + new Vector3(-0.5f, 0, -0.5f), Vector3.down);
bool cast1 = Physics.Raycast(ray1, 0.50f);
bool cast2 = Physics.Raycast(ray2, 0.50f);
bool cast3 = Physics.Raycast(ray3, 0.50f);
bool cast4 = Physics.Raycast(ray4, 0.50f);
if (cast1 || cast2 || cast3 || cast4)
rig.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
void move ()
{
float xIN = Input.GetAxis("Horizontal");
float zIN = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(xIN, 0, zIN) * moveSpeed;
dir.y = rig.velocity.y;
rig.velocity = dir;
}
I don't really know if this is the best way for jump, but it should still work I guess. So, why if I use Input.GetButtonDown("Jump") it's ok? What do I have to add in the code? Thank you!
You do have if(Input.Get$$anonymous$$ey("space"))
calling tryJump()
every frame space is pressed, maybe that's the issue. Use Input.Gey$$anonymous$$eyDown
ins$$anonymous$$d.
Hi, Thanks for the answer, but nope, it doesn't work, because Get$$anonymous$$eyDown get only first frame, so if I keep space pressed, my character doesn't autojump, he jump just the first time when I press it (the description of Get$$anonymous$$eyDown is "returns true during the frame the user starts pressing down the key", while the description of Get$$anonymous$$ey is "returns true while the user holds down the key"). How should I fix this? Could it be a problem with Ray or with physics in Unity or other things maybe? Thanks again!
Ah, I misunderstood. It would be better to use isGrounded
to check if the player has landed before jumping.
if(Input.Get$$anonymous$$ey("space") && GetComponent<CharacterController>().isGrounded)
{
tryJump();
}
Answer by LeFlop2001 · Feb 02, 2020 at 04:30 PM
Im pretty sure you’re having this issue because you’re using getKey and not getKeyDown. So instead of adding the force on the frame you press the button, it continues adding force till the raycasts no longer hit. This is different based on the situation.
Hi, maybe I said that badly, I wanna make an autojump system: when the player hold down "space", it jump again, again and again, until he stops. The problem with get$$anonymous$$eyDown is that the first frame when you start holding space is the input. In that case I call the function only one time (the first frame) and that's ok but for a normal jump system, not for an autojump. (Btw I tried, not telling you something I didn't try)
$$anonymous$$aybe you can still do it with get$$anonymous$$eyDown but you have to do lot more useless work (as you have only 1 frame as input you have to create bool isJumping, float maxAltitude, bool $$anonymous$$ax_Height_Reached and you want to move the transform.position with a float gravity etc.)
With get$$anonymous$$ey you have as input all frames where you hold "space", so I have only to check if the character is on the ground or not.
To be clearer I will put a gif with the glitch:
As you can see, the character does autojump (I was holding "space") but at different altitude. Is it caused by the update function? because it depends on pc's performance and not on real ti$$anonymous$$g and maybe sometimes the input get 2-3 times in 1 frame? I really don't know what is the problem of that simple code. In my code the raycast hit only when the character hits the ground, so the hitbox is correct. The force is added only when ray (boolean) hits the ground, so midair is not a problem (in theory) Hope that explanation was better. Thanks anyway!
What I meant regarding the raycasts is that because the raycasts have a length the jump gets inconsistent. After jumping you add the force shooting it upwards but based on what velocity or momentum the body already had it could happen that during the next frame the body is still close enough to the ground for the raycasts to hit. Thus it adds the force again. I would suggest using the oncollision function and a simple bool to handle the autojump
bool grounded = true;
void Update()
{
If(grounded&&Input.get$$anonymous$$ey(keyCode.space))
{
rig.AddForce(Vector3.up)
grounded =false;
}
}
void OnCollision(Collider collision)
{
If(collision.transform.gameObject.tag = „floor“)
{
grounded = true;
}
}
Truee I will try today. Just to know, is there a way to make it not based on frame but on time? Like Time.deltaTime or something like that ? If so where should I add it? Thanks so much, trying today
Your answer
![](https://koobas.hobune.stream/wayback/20220612222546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding a jump feature help? 2 Answers
Jumping causes strange bugs and messages 3 Answers
Others can't jump in my game 0 Answers
Anti-Gravity isn't working 1 Answer
Hey Guys I have a problem with a single jump script. 1 Answer