- Home /
when 2d character hits wall it shakes/bounces
i am new to this and am making a small 2d game to learn. when my character hits the wall it shakes or bounces if you like. in my first level i made constraints in the script to stop this. however this level is not a square like the first level. is there any way i can make a wall without it making the character shake?
Do you mean the character is see$$anonymous$$gly entering the wall and then being placed back outside of the wall repeatedly?
I'm assu$$anonymous$$g you are using Translation for your movement then?
Yeah, Translation will move your character in set amounts, so if your characters position + your movement value is within the wall, it will move you within the wall instantly, and then resolve the collision pushing you back out.
There are two ways you could go. You could either perform a check before moving to see if you would intersect a wall, and then alter your movement value to make sure that you do not ever end up inside of a wall, or you could switch your movement code to using velocities, however this could require a significant amount of work depending on how sophisticated your movement code already is.
Answer by Sospitas · Jul 17, 2013 at 09:56 AM
You could create a Ray from the players centre to either side, and then calculate the length of that ray. If the length is < your translation value, then adjust the translation value to match the distance (probably have to account for your collider bounds as well), something like this should suffice:
function CheckMovement()
{
RaycastHit hitL, hitR;
if(Physics.Raycast(player.transform.position, Vector3.left, out hitL))
{
if(hitL.distance < translationMovementValue)
{
translationMovementValue = hitL.distance;
}
}
if(Physics.Raycast(player.transform.position, Vector3.right, out hitR))
{
if(hitR.distance < translationMovementValue)
{
translationMovementValue = hitR.distance;
}
}
}
To be honest, you could probably split that up so that it will only Raycast to the left or to the right when you press the corresponding movement key, which would be better. But that should be a good starting point for now :)
Your answer
Follow this Question
Related Questions
2D Character Switch Systsem (Same Location) 1 Answer
Non-symmetrical Character walk left animation. 0 Answers
"wheel" character 2 Answers
Android 2D Character Controller 2 Answers