- Home /
2D motion/collision: physics, or direct translation?
Found out the hard way today that if you simply translate an object by calling its position.transform and setting it to some other vector2 (which I've been doing to move my character), collisions don't trigger. To actually make them trigger, you need to do it in the physics. So instead of directly altering the character's x,y position (this is a pixel-based flat 2d game, by the way, like original Zelda) you need to alter its velocity, which isn't as pretty.
But even worse than having to get your hands dirty with velocity is how, to make physics collisions work, your rigidbody can't be kinematic. Meaning gravity will effect it. Of course, there are ways around this, but by this point I feel like I'm going very very out of my way to make something as simple as a collider work (this is an old-school flat 2d game, anyway -- I don't even need true physics at all).
I'm wondering what is the best way to do this?
I have a character sprite walking along the floor. I don't want him to be able to walk through the wall. What's the simplest way to do this?
Thanks.
EDIT: I just saw these two hints, but neither of them work for me:
If you are directly manipulating the Transform component of your object but still want physics, attach a Rigidbody and make it Kinematic.
If you are moving a GameObject through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.
If I make my character kinematic and then alter its Transform.position to relocate it pixel by pixel, eventually hitting a collider, nothing happens: the object goes right through it, and the collider method isn't even called.
Answer by Noob_Vulcan · Jul 10, 2014 at 04:24 AM
Collision Detection is done by Physics Engine. So to detect collision
a.You should have a Rigidbody component attached to the game object that has the script containing the OnCollisionXXXX, OnTriggerXXXX methods on it.
b. U can set gravity to false of any rigidbody.
In Your case ... Make Character Rigidbody with gravity on/off as you wish. Dont Set isKinematic=True. And Move your character by adding force to it.
Or You can use Character Controller if you dont want rigidbody i.e Physics .
Thanks. What I ended up doing was setting gravity to 0, and turning is$$anonymous$$enematic=false. I don't move the character by adding force, though -- turns out in this case you can use transform.translate (and maybe also even transform.position) and collisions will trigger as long as kinematic=false.
Answer by Pyrian · Jul 10, 2014 at 04:47 AM
If all you want is to stop moving when you hit a wall, but you're otherwise happy with your movement code, consider using Triggers. Then, you can detect the "collision" in kinematic (4.5+) without any real physics going on. Keep in mind that you'll overlap, so when you catch the trigger, you'll have to back up slightly.
Thanks! Are you saying that when kinematic=true, even though OnCollision won't be called, OnTrigger still will be?
Pyrian, while I have you here, one more relevant question:
If Rigidbodies exist to apply forces to gameobjects, and enabling is$$anonymous$$inematic disables all forces from interacting with the object, then... what's the point of a Rigidbody with is$$anonymous$$inematic set to true?
As opposed to simply not having a Rigidbody2D? If you want to move a collider, you should have a Rigidbody2D - any colliders not associated with a Rigidbody2D are considered static colliders, and there is a high overhead cost with moving them (I imagine this is because of the way they're indexed?).
Also, the kinematic setting is useful for triggering physics on and off for an object without the overhead of adding/removing the component.
If the object has no colliders or does not move, nor is expected to ever interact with physics forces, then a kinematic Rigidbody2D would be entirely superfluous.
Your answer
Follow this Question
Related Questions
Is there a solution to when colliders bypass? 2 Answers
Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers
Unity 4.3 CharacterController collision with physics 2D not working 1 Answer
Rigidbody and Parenting - Kinematic Rigidbodies? 2 Answers
[SOLVED] Desactivate pushing forces between two objects 2 Answers