- Home /
2D box collider (character floating)
i have been reading other questions similar to this one, but none of them have worked for me, my problem is that my character seems to be floating and it cant jump since if not on ground, jump = false. Here are some screen, the first one is before i run unity, the second one is when its running.
I'm not sure if the size of the sprite has anything to do with it, since I´m using really small images (13x19). Any help is useful thx.
Can we see your jump code there may be something in there that is setting some different values.
Also note t that if your character does not have a rigidbody he won't be affected by gravity and thus be pulled towards the ground.
Your sprite size itself should not have any effect because Unity is perfect ally happy just meshing different objects together in world space so if you put him in the ground Unity would say that's O$$anonymous$$ that's where you want him.
So it has to be something in your settings or in your code.
var onGround : boolean;
private var jump : boolean;
private var vm : float;
var jumpSpeed : float = 25;
var fallSpeed : float = 64;
function FixedUpdate(){
if(onGround && jump){ //Are we grounded can we jump?
vm = jumpSpeed;
}
else {
vm -= fallSpeed * Time.deltaTime;
}
vm = $$anonymous$$athf.Clamp(vm, -maxFallSpeed, maxUpSpeed); //Clamp vertical speeds
rigidbody2D.velocity = Vector2.zero; //Stop movement if there is no input
onGround = false; //Clear out grounding check
Also note t that if your character does not have a rigidbody he won't be affected by gravity and thus be pulled towards the ground. (Done).
Here is some part of the code (jump movement).
Answer by oasisunknown · Jun 06, 2014 at 10:47 PM
Here are some things to note
vm -= fallSpeed * Time.deltaTime;
this does not need Time.deltaTime because your running it in fixed update. fixed update runs at a fixed step count and is not the same as the Update() function. Update runs as fast as the computer can go where FixedUpdate runs at a constant rate for physics reasons regardless of frame rate.
Next,
rigidbody2D.velocity = Vector2.zero; //Stop movement if there is no input
This is constantly going to set your velocity to zero because it is run every time the FixedUpdate runs. encapsulate it in an if statement that says something like if not jumping then do this.
onGround = false; //Clear out grounding check
I never see where you set this back to true. so in the code snippet its always false and always returns not grounded. Even if you set it to true in some other part of the code when it gets to it here it will just change it back to false.
Lastly I am going to link you to the Unity Learn tutorial that I watched about ground checks and jumping that Mike Geige did. He writes it in C# but the principles are the same.
The coding starts at around minute 38. everything before that is set up.
Please help, that script is outdated and I can't fix my problem!
Your answer
Follow this Question
Related Questions
can anyone help me to smooth the jump? 0 Answers
controller.Move not working correctly 0 Answers
tring to make a jump pad in fps 1 Answer
How animations are executed ?? 1 Answer