1
\$\begingroup\$

I don't really fully understand how to use rigidbody and which movement functions in a 2D game. There seems to be so many options and it's just a mess in my head right now. If I'm making a game like those classic 2D zelda games, do I need rigidbody at all? I don't think I will need any physics but I obviously need to check if two objects have collided so I can do some logic on them, like destroying an object on collision or hurt the player, or stop movement if player walks into a wall.

Is going with no Rigidbody and moving only with transform.translate a good choice with these types of games?

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Rigidbodies are required when you want the engine to handle collision detection for you. If you want to prevent your character from walking through other objects with colliders (like walls), then you either have to check for walls in your own movement code or you need to add a rigidbody to your player.

And if you want to use trigger areas which do something when the player walks into them, then that's another reason to add a rigidbody to the player, because that's a requirement for causing OnTriggerEnter events. For more information about the Unity collision rules, check the tables under "Collision action matrix" in the manual article about colliders.

Now about how to move your rigidbody. I created a little demo game which demonstrates the effects of different ways to move a player-character. You can check it out on itch.io.

When an object has a non-kinematic rigidbody, then you should generally not manipulate the transform directly. When you change the transform, you are not moving the object, you are teleporting the object. This ignores collision and can cause weird effects if you teleport an object into another object with a rigidbody.

If you want physics in your game (objects pushing each other and getting pushed in a physically correct way), then the cleanest and most recommendable way is to control objects with Rigidbody.AddForce. When you feel like your objects behave too much like they are gliding on ice that way, increase the drag value of your rigidbodies. The result will be that your objects move with a bit of momentum, which often looks and feels pretty nice.

If you are looking for something in between the physically correct movement of handling forces and the tight control of manipulating the transform, manipulate the velocity property of the rigidbody. That way you still have collision handling and can push things, but the object itself won't behave in a physically corret way when interacting with other objects.

\$\endgroup\$
2
  • \$\begingroup\$ Good answer. Just to add that in general working against unity is generally ill advised. Some of my biggest mistakes early were trying to ignore unity systems and implement them myself. But quickly you'll find Unity doesn't play nice with that. It's best to use Unity's built in systems whenever you can. \$\endgroup\$
    – gjh33
    Apr 4, 2019 at 15:38
  • \$\begingroup\$ Thanks a lot Philipp! That game really helped me understand it better. I think I'll go with moveposition as I'm not really interested in the slippery movement that comes with acceleration / deceleration (I don't think this is used in the zelda type of game). I'll keep those other alternatives in mind if I ever make some kind of vehicle in a game like a car that has that kind of movement. \$\endgroup\$
    – Patrician
    Apr 9, 2019 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .