[2D] Slide object after collision
Hello community!
I am currently working on a top down 2D game and I got a little problem.
I want to make an object slide AFTER a collision. So that the player changes the direction (with the "W-A-S-D" keys), but the object which was hit before slides a bit in the previous direction.
Here is my code so far, but it doesn't work: (for example, when the player pushed the object to the right):
private void OnCollisionExit(Collision collision)
{
collision.transform.Translate (Vector3.right*10);
}
I hope you understand what I want to do and thanks in advance!
EDIT: It is a top down game, so there is no gravity.
EDIT2: I added a few images.
"So that the player changes the direction, but the object which was hit before slides a bit after that direction change because that object doesn't change their direction."
Could you rephrase this? i dont think i understand it fully..
Ohhhhh do you mean that the phyisics dont fit into the grid properly???? So what you want is that that object fits into the grid perfectly
The method is just an example. Is there a better way to slide an object ?
Answer by $$anonymous$$ · Mar 04, 2017 at 01:49 PM
Honestly, the best way I see moving this object is through the physics supplied in the engine... make the mass of the Player larger than the box, and when the character moves and collides with the object.. the object will slide via being pushed by the character...
No need for to re-invent the wheel heres a previous question that has created a movement script for a top down style game: http://answers.unity3d.com/questions/883081/2d-top-down-character-controller.html
Answer by xm4str33 · May 06 at 10:13 AM
This works for 3D games
Go to your player's rigid body > increase the Drag number
Go to your player's rigid body > Constraints > Freeze Rotation on XYZ and Freeze Position on Y