- Home /
2D rigidbody movement & walls
Im having some issues with my 2d movement in my game.
If the character jumps while moving into a wall, they will get stuck, with the collider being slightly inside the wall.
The gameobject has a rigidbody as well as a box collider. The walls have a collider. I have been told that using Translate is not the proper way to move rigidbodys, but the project is tens of thousands of lines long and changing the way movement works just isnt something that I want to consider at this point. So a way to check the surrounding areas to make sure the player isnt about to move into a wall is what I need.
movement = 1;
movement *= gameData.GetDelta();
thisTransform.Translate(movement, 0, 0);
Answer by DESTRUKTORR · May 13, 2013 at 09:04 PM
The reason that translate isn't a proper way to do it is because it doesn't apply the physics calculations during interactions between colliders. That is to say, you're trying to reinvent the wheel, with the physics engine. I'd hate to say it, but your best bet (without changing to a velocity/force-based movement system) would be to check collisions and apply counter-force.
Why would you assign movement to 1 then multiply it by something...? 1*(someNumber) = someNumber... if you've programmed like this throughout the project, you'll probably want to go back and change a lot of things, anyway. Also, is this a monobehaviour? If so, why have you used up another reference to refer to the transform? You should change this all down to the following:
transform.Translate(gameData.GetDelta(),0,0);
However, the most efficient way to manually check if the player is moving into a wall is to check it on the frame in which it occurs, or to do a raycast/spherecast in the direction of movement. However, this can get very expensive very quickly, and I STRONGLY suggest you change to velocity/force-based movement.
Answer by Superwayne · May 13, 2013 at 09:21 PM
The only thing I could imagine would be: Create a new script for the wall with "OnCollisionEnter", check for the player GameObject and translate it to a place, where it's not stuck in the wall. With a simple if/else you could chose on which side the player should be translated by comparing player.transform.position.x and transform.position.x (considering x is your horizontal axis)
This is not an optimal solution but the best I can imagine without changing your movement..
Your answer
Follow this Question
Related Questions
Making character dash forward 0 Answers
Character Controller Follow me in the air 1 Answer
A node in a childnode? 1 Answer
jump script : 2D 1 Answer
Cannot move elements in 2D scene. Help 3 Answers