overlapsphere and tested for grounded or not

Hi, I have a problem with my character jumping with the below code. The character jumps but if i press the jump key down it jumps the character quite high and if i tap jump key really quickly it jumps how I would like it too.

function FixedUpdate() {

var hitColliders = Physics.OverlapSphere(groundCheck.position,groundCheckRadius,whatIsGround);
grounded_1=hitColliders.Length >0;

}



//in my update function i have

if (Input.GetButtonDown("Jump") && grounded_1==true)
   {

     //character jumps
     rb.velocity.y=15000*Time.deltaTime;

   }

I’ve got colliders on the objects (just one collider) i wish to collide with, not sure whats going wrong here, any ideas?

Thanks

just tested again and it mostly works, jumps as I want it but now and again my character jumps double the height…

Why are you multiplying by Time.deltaTime? You’ll get varying strength of jump depending your framerate.

It also makes the amount of jump speed less intelligible. What is 15000? Well if we’re at 60fps we end up with a vertical speed of 250 units/second… but if framerate drops to 30, we have a speed of 500 units/second.

If you expect a speed of 250, just set it to 250.

As for you sphere testing. If you just want to test if a sphere overlaps anything, use Physics.CheckSphere:

You seem to just overlap, and test if the collider count is greater than 0. Well CheckSphere returns true in that situation… and doesn’t incur the cost of returning all that extra data about who was overlapped. Essentially making it speedier.

1 Like

Thanks for your response, I’ve got rid of Time.deltaTime and replaced it with a more normal value of 200. I changed the other line to
grounded_1=Physics.CheckSphere(groundCheck.position, groundCheckRadius, whatIsGround);

it works but again if i press jump now and again it jumps really high

appreciate the above changes as thats helped alot but I need to find out why its jumping high now and again…

Thanks
Dave

I’ve tried a few things to get this jumping more consistent but cannot manage to solve it yet.

Tried
rb.AddForce(Vector2.up*6000); (not sure why I had to put such a high value to get it jumping normal)

also tried

rb.velocity=Vector3(0,200,0);

jumping still sometimes jumps higher than it should…

ok sorry guys, in my code further up the page i had a line affecting the velocity which was causing the problem

Thanks

Another guess would be, that your character is still grounded while jumping codewise, so your code thinks, he can jump again and that doubles your height. Maybe try debug.log with a coroutine or sth to check while he is in the air, if he is still grounded.

  1. put a debug line into where you set the velocity, make sure you set it only once

  2. you’re setting the velocity, Unity suggests to not do this (though I seldom see an issue doing it personally…). They say to use ‘AddForce’, there’s a ForceMode.VelocityChange that you can pass into AddForce for setting the velocity as opposed to impulse or force or what not.
    Unity - Scripting API: Rigidbody.AddForce

  3. you shouldn’t be setting the velocity in ‘Update’, but rather in FixedUpdate. BUT Input doesn’t register properly in FixedUpdate, which is why I bet you have it in ‘Update’… I know… it’s annoying. You can cache the command to jump into a boolean, and then next FixedUpdate apply the jump to the rigidbody.