- Home /
rigidbody2d.velocity isn't doing anything.
Hello, my character is supposed to be able to jump with a rigidbody.velocity script. I have a rigidbody component for character, but it still won't do anything. Also the console shows no errors. What is wrong?
//jumping
if(canJump == true){
rigidbody2D.velocity.y =10;
Debug.Log("This Shows up")
}
}
function OnCollisionStay2D(coll: Collision2D) {
if(coll.gameObject.tag == "Terrain" && Input.GetKeyDown(KeyCode.W)){
canJump = true;
}
else{
canJump = false;
}
}
I'm pressing "w" , and the log shows up, but my character does not jump. Please help, this has been stumping me for the past few days.
UPDATE UPDATE
The programming part look's like it has no problems. Here's my project so you can see.
https://www.mediafire.com/?ro0rmr0lx28epgo
A and D - left/right W - Should make you jump if we get the problem figured out.
Yeah, and I know it's simple. Just testing out things really.
Thank you.
Would you be able to provide a sample project that shows this problem? From the code you posted it looks like it should work.
Use rigidbody2D.AddForce ins$$anonymous$$d.
function OnCollisionStay2D(coll: Collision2D)
{
if ((coll.gameObject.tag == "Terrain") && (Input.Get$$anonymous$$eyDown("w")))
{
rigidbody2D.AddForce(new Vector2(0,100));
}
}
Simplified the code too. No need to be swapping around a bool, when you already check to see if the person is on terrain. If they are pressing "w" while they are colliding with the ground, then you make them jump. No bool needed.
Thanks for the simplified version, BUT addforce isnt see$$anonymous$$g to do anything either. I'm going to upload my project to my question, as this doesn't seem to be a program$$anonymous$$g issue.
Try adding a Debug log to the on collision function to see if it's even being triggered at all.
Answer by ncallaway · Jan 06, 2014 at 02:14 AM
The problem is that Turtle has an Animator on it with "Apply Root Motion" checked. This means the Animator is constantly updating the position of the turtle to be in a position as defined by the animation it's playing.
If you uncheck Apply Root Motion from the Animator, your jump code starts working.
I also used rigidbody2d.velocity.y = 10 instead of rigidbody2d.AddForce().
I also found that "Gravity Scale" on the Turtle's rigidbody was quite strong, so that the Turtle wasn't able to jump very high, so you'll probably want to play with that number a bit.
Oh my! Thank you so much. I thought it was something small that was in the way. Have a good one!
Holy crap ncallaway, thank you so much, I've been $$anonymous$$ring my hair out trying to figure out why rigidbody2D.AddForce() AND rigidbody2D.velocity hasn't been working! Thankyouthankyouthankyouthankyouthankyouthankyou
Answer by superluigi · Jan 06, 2014 at 01:37 AM
This might sound a little crazy but try
function OnCollisionStay2D(coll: Collision2D)
{
if(coll.gameObject.tag == "Terrain" && Input.GetKeyDown(KeyCode.W))
{
canJump = true;
rigidbody2D.velocity.y =10;
}
}
Let me know if you get something. Also if you don't mind post your entire code.
I posted my whole entire project so the whole script is in there. Thank you for trying to help.
Ok but did u try m suggestion? I think it is a problem with ur code and that it'll work.