So I’m developing this Roguelike where the player moves using transform.position (and I’ve tried using transform.Translate()) although it could use any similar function. NPCs use the NavMesh agent, so they’re not having this problem.
Whenever I get enough movement speed boost (or I want the player to dash, haven’t implemented any dash abilities yet due to this and I’m DYING to do it), the following scenario could happen when using transform.Translate or moving transfrom.position (sorry for the poor drawing):
It can get through the wall. I want to make a system so that, no matter how long the dash is or how much the movement speed becomes, if there’s a wall, no matter how thin it is, it won’t let the player go through.
How can I achieve this?
You don’t modify the Transform.position if using 2D physics. You use a Rigidbody2D and set its velocity or add forces. Using Continuous collision detection it won’t go through colliders.
Rigidbody2D are not renderers, you don’t control them with the Transform.
Thank you for your answer, MelvMay. So, in order to move the player, I should modify the velocity / forces of the Rigidbody2D attached to it and that will always respect any nearby Rigidbodies/Colliders, is that right?
Yes. The Rigidbody2D is in control of the Transform. When the Rigidbody pose updates due to it having velocity, contacts, joints (etc) it then updates the Transform.
In short, changing the Transform bypasses what the Rigidbody2D is trying to do.
I’d suggest following a few 2D physics tutorials because this is the first thing that would be covered and might save you struggling with issues like this.
Thank you! I’m gonna check those tutorials. Are there any Unity official tutorials on the matter? I’ve seen quite a lot of Unity tutorials by themselves but haven’t seen any about Physics 2D.