- Home /
sticky and blocking colliders that stop my character from running around.
Hi, everybody !
In the many plaformers that i've tried to devellop, there is a problem with the collider walls my rigidbody player (he is not a character controller) walks in. When touching a collider , my player cannot jump, he stays blocked near this collider until you release the move arrows, or the jump key. I would be more specific, but my problem couldn't be fixed with a code i think. Maybe there's a Rigidbody variable I mistreated.
Thanks for reading !
Did you add a Physic material to the colliders? You can get sample Physic materials by Assets>Import Package>Physic $$anonymous$$aterials. Start with something slippery like 'Ice'. You can create your own materials with the properties you need.
There is a 'Frictionless' Physic material in AngryBots tutorial.
Alright thanks i'll chek it out. Didn't know much about physics materials.
Answer by donnysobonny · Aug 31, 2013 at 01:05 PM
I have had this problem numerous times also within project developed inside of Unity. The problem is not due to the physics material, it is because when you are holding the arrow button, the engine is physically trying to push your object through the wall and the physics engine is trying to correct it by pushing it back away from the wall at the same time. This ultimately leads to the problem that you've described and is something that really should be avoided using a collision detection system.
The easiest and most optimised way to do this would be to cast a ver short ray directly infront and behind of your character. If the ray infront collides with something, disable forward movement, if the ray behind collides with something, disable backwards movement.
You could even take this one step further and use the above method to trigger animations to simulate bumping into a collider.
Actually thanks to the friction less material. $$anonymous$$y character is much more " slippery i guess. It sorted a lot of things and it's now much more playable.
Answer by acornies · Jun 04, 2014 at 03:44 PM
I solved this "sticky" ridgidbody issue using the solution suggested by @donnysobonny.
The easiest and most optimised way to do this would be to cast a ver short ray directly infront and behind of your character.
Here's some code that I put in FixedUpdate() to solve it. Keep in mind that my game is 2.5D using the Unity 2D view on the x and y axis.
rigidbody.velocity = new Vector2(
this.MoveDirection * this.MaxSpeed, rigidbody.velocity.y);
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.right, out hit, 0.1f)
|| Physics.Raycast(transform.position, Vector3.left, out hit, 0.1f)) {
Debug.Log("Hit " + hit.transform.gameObject.name);
if (hit.transform.tag == "Stoppable"){
rigidbody.velocity = new Vector2(0, rigidbody.velocity.y);
}
}
In this example, I've tagged the objects I'm colliding with using a custom "Stoppable" tag. This can easily be changed to hit.transform.name, layer or some other identifier. I'm also using my own "MaxSpeed" and "MoveDirection" variables to handle the initial ridgidbody velocity.
Hello there. I am new to unity, can you $$anonymous$$ch me how to do it? Can you email me? fajardo.timothy@gmail.com
this is my last bug in my game for our thesis, I hope you can help me. Thank you in advance
Your answer
Follow this Question
Related Questions
Need help when arrow hits collider 1 Answer
2 cubes go through each other instead of stopping 0 Answers
Internal collisions 1 Answer
Wheel Collider General Friction 1 Answer