- Home /
Workaround-ed
Rigidbody addForce when colliding with 2 mesh colliders
I have a character with a rigidbody and a box collider, and I am using mesh colliders along different chunks of ground to walk on. The character jumps with rigidbody.AddForce(Vector3.up * jumpForce). This works fine when the character is on a single chunk (middle or corner), but if he stands at a point where two sections meet (where he collides with both), his jump height gets cut down (about 1/4 or less what it should be). Does anyone know what might be causing this issue?
Without a more detailed description/visualization of where your colliders are in relation to the character this is very hard to say. It sounds to me that the character is not affected by anything when jumping and would just land again on the ground where he took off (the mesh colliders). If this is so then even friction (which is the first thought I had) would not come into effect. But if for example he moves sideways while/before jumping maybe friction comes into play here... please post more detail.
Thanks for the initial replies, I'll try to give a better picture of what is going on.
There are 3 objects: Character: with a rigidbody and a box collider Ground 1: with a mesh collider Ground 2: with a mesh collider
Ground 1 & 2 are below Character (with respect to y), and Character is being held to them by gravity.
The relevant code (JS) is: var jumpForce : float = 4000; rigidbody.AddForce(Vector3.up * jumpForce);
If Character is standing anywhere on Ground 1, and not touching Ground 2, the jump works as expected. The same if he is on Ground 2, not touching Ground 1
If Character is touching Ground 1 and Ground 2, and jumps, the jump height is cut down significantly. This occurs regardless of x movement (the game is 2D, so z movement is not involved).
I see this issue whether or not Ground 1 & 2 are touching each other.
As mentioned, the game is 2D. I am using 2D Toolkit for sprites, and all colliders are their normal 3D versions, since development began before the introduction of 2D colliders.
Ok, got it. Preferably you call all Physics related methods in FixedUpdate, yes. Please read the manual regarding this. It really is only required if you want to apply a force over several frames. The Input.GetButtonDown should be called in Update though - as you did. This becomes relevant if the game frame rate is higher than the Physics engine frame rate. You want to receive the GetButtonDown event and this can only be assured in Update. You would then for example set a state which is then queried in FixedUpdate and triggers the jump. But you can leave it as it is for now. Have you looked into the friction settings? Are any Physics $$anonymous$$aterials involved?
Answer by 2d4Games · Mar 28, 2014 at 11:16 AM
This isn't really a solution, more of a work-around.
I was able to rework the script to set the velocity directly, rather than add a force to the rigidbody. This new method does not have the issue.
Thanks everyone for the help in debugging this problem.
Interesting. It may be worth noting that I think the proper way to do a jump by using AddForce is to pass in the optional second parameter in the AddForce function to Force$$anonymous$$ode.Impulse. I'm not sure if that would also fix your issue then, considering setting the velocity directly did.
Follow this Question
Related Questions
Trouble in freezing movement in z axis 1 Answer
AddForce to opposite direction 2 Answers
Addforce to ball in camera position 0 Answers
Add Force when level loads 2 Answers
AddForce wont apply force to gameobject...help please 1 Answer