- Home /
Object jumping while moving(with addForce)
I have a level build with 3d tiles, and when a player move with rigidbody.AddForce(with any ForceMode) he starts jumping:
All tiles(simple cubes) has size 1.5 and placed properly(cube 1 pos.x = 0, cube 2 pos.x = 1.5 and etc.). If i remove colliders from tiles and make only 1 big collider, all work fine.
Movement code:
public float Speed = 10;
public ForceMode Mode = ForceMode.Acceleration;
public KeyCode KeyRight = KeyCode.D;
public KeyCode KeyLeft = KeyCode.A;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyRight))
{
rigidbody.AddForce(Vector3.right * Speed, Mode);
}
if (Input.GetKey(KeyLeft))
{
rigidbody.AddForce(Vector3.left * Speed, Mode);
}
}
Player object:
Anyone know how to remove this jumps?
Try to freeze position in x axes. Box jumpes cause u freeze z axes :)
I agree with Chariot. But I think that it's the y-axis that should be frozen. Would it break your game to unfreeze the y-axis only when the player jumps or something makes the player go flying - and then re-freezing it once you're grounded again?
In the project I did it, but now I want to find a solution without freezing Y position.
Answer by Simon-Larsen · Jan 17, 2015 at 05:20 PM
Replace the box collider on your player with a sphere collider or capsule collider and all should be good :)
i am having the same problem with a capsule collider in Unity 5.0.1f1 i am trying to make an endless runner where my character walks and the road is built up with objects that have a collidersame scenario as stated above, sometimes it works sometimes it just hops up. i cannot contra
Answer by hav_ngs_ru · Jan 19, 2015 at 09:08 AM
i guess it jumps because of friction that causes some torque, and rude rotation constraint calculations..
try set angular drag to any huge value first.
if it not help - try setup custom physics material: set allfrictions to 0, and set drag greater than 0 (in other words - replace surface friction by drag).
Doesnt help, still jumps. I think the problem in several colliders, since with one all right. But do not know how to solve it, because one сollider does not suit me.
seems like you are right, your cube clings to tiles corners... try to uncheck z-rotation constraint to test it (and set friction and drag settings as it was before). If cube will cling corners and somersault ins$$anonymous$$d of jumping - assumption will be confirmed, and then we'll try to find solution...
are your tiles placed with exactly same y-coords and scale? are they closely adjacent to each other? couldn't it be some roughness on junctions or gaps therebetween?
Yes, tiles placed correctly, they all with same scale and placed with grid snap. Uncheck z-rotation(same behavior with one collider and tiles colliders):
well... seems like it's because of a physics inaccuracy, because I dont have any assumptions :)
so question is not like "how to fix" but "how to workaround this bug"... I have one idea, but it needs to be confirmed... lets make one more experiment: sphere collider with no constraint on z-rotation. if it will just rotate (not jump) - you could make 2 gameobjects connected with hinge joint: first one is invisible, with sphere collider with no z-rotation constraint, and second one is visible box with no collider and with constrainted z-rotation. First one will smoothly roll, nd second will make illusion of sliding...
but if it will still jump... then i'm afraid that it will be better for you to review you scene architecture (possible you will need to make a single box collider to wrap you in-line tiles at runtime, to make your floor absolutely flat, or smh like this...)
Your answer
Follow this Question
Related Questions
How do I add force To rigidbody based On Character's Walking direction? 0 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Erratic Physics behaviour while testing in 2 computers 0 Answers
Prevent Rigidbody From Rotating 3 Answers
How to get spring to settle? 0 Answers