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.

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.

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 .