- Home /
The question is answered, right answer was accepted
Prevent Object/Character from "drifting" away from Collider
Hey folks!
I am currently working on a 2D MMO RPG!
And I am sort of running into a little "problem"..
Here's the thing: I have a gameobject (the Player) who is moved using the transform.Translate function, everything works fine and all, but if a player runs against a collider which stops the player from moving (a simple box collider) the collider also creates velocity so that the player starts to "drift" away from the colliderbox... Now I do use Sleep() and WakeUp() when the player lets go of all the movement buttons, so the player stands still again, which works fine, but it doesn't solve the problem if the player keeps holding a movement key after running against a wall (which can make a player significantly faster..)
If you need any exact codelines, just tell me, and I shall add them!
Cerbion
Does your player and collider have a parent/child relationship or is it all one contained GameObject?
Well! (I had to check on this one ._.)
The Character consists of a couple of Game objects, but the main one (the parent object) has both the Collider and the $$anonymous$$ovement Script attached to it!
// EDIT: screenshot: http://puu.sh/6Fw72.png
I think misunderstood you. I thought you were saying the player and its simple collider are 'drifting' away from each other. You are actually saying when this player hits a different collider (wall) he gains some kind of acceleration bonus. Is that right?
If so, could you post the relevant code on the object collider that's adding velocity?
Sorry if I my writing led to this misunderstanding Q_Q english is not my native language :I
Anyways! Here you go:
// VERTICAL AND HORIZONTAL $$anonymous$$OVE$$anonymous$$ENT
if(moving == "left") // $$anonymous$$ove Character LEFT
{
velocity = new Vector3(1 / acceleration_normal, 0 , 0);
$$anonymous$$ovePlayer("left");
}
if(moving == "right") // $$anonymous$$ove Character RIGHT
{
velocity = new Vector3(-1 / acceleration_normal, 0 , 0);
$$anonymous$$ovePlayer("right");
}
if(moving == "up") // $$anonymous$$ove Character UP
{
velocity = new Vector3(0, 0 , -1 / acceleration_normal);
$$anonymous$$ovePlayer("up");
}
if(moving == "down") //$$anonymous$$ove Character DOWN
{
velocity = new Vector3(0, 0 , 1 / acceleration_normal);
$$anonymous$$ovePlayer("down");
}
if(moving == "stop")
{
velocity = Vector3.zero;
$$anonymous$$ovePlayer("stop");
}
if(velocity != Vector3.zero)
{
transform.Translate(velocity);
}
else
{
transform.rigidbody.Sleep();
}
Answer by Cerbion_ · May 13, 2014 at 12:07 PM
I know this thread is old, but I fixed it on my own:
I had no Gravity/Drag enabled on my rigidbody (because the game's only 2D) but with the gravity and drag/angular drag enabled and set to a moderate amount, everything works and the character is not getting pushed away from walls anymore!