Hello,
I’ve been going through Unity’s main feature set and have managed to do pretty much everything I tried, small simulations, basic game mechanics etc. However I haven’t really gone deep into the Physics part and now, after managing relatively complex scripts, I’m surprised to be stumped by a very easy problem which I can’t seem to solve.
The setup: 2D project. Player character is a sprite with a movement script attached. It also has a Box2D Collider component. Another hand-drawn sprite acts as the background, which is top-down view of a room with four walls. In several places there are openings which are supposed to lead to other rooms (Unity scenes). If I put empty Gameobjects with BoxCollider2Ds on top of these openings I can detect when the Player collides with them and do scenes transitions. But for the life of me I can’t figure out how to prevent the Player from drifting out through the walls.
I tried adding Rigidbodies, other BoxCollider2Ds etc. I’ve watched countless maze tutorials. My Player movement script uses explicit transform.position which I suppose should be changed to use AddForce or velocity vectors instead. Some tutorials use raycasting to check if a wall is present or not. It all seems unnecessarily complex.
I’d like to know if there’s a way to simply stop the Player from going through an empty GameObject which has a BoxCollider2D attached, which is all I need.
Thanks a lot.
Modifying the transform.position directly will cause the object to pass through everything. Try using the rigidbody movement functions, like AddForce (like you already mentioned). Make sure you’re moving it in FixedUpdate (though I’m sure you know that), and that the object you’re colliding with has a collider.
Hopefully that’s what you needed and I’m not spewing obvious uselessness 
Edit: Don’t know why I said rigidbody.MovePosition, that would still clip through.
Thanks a lot. No, this was not useless at all… It confirmed what I was guessing, that the Player object needs to be moved with AddForce. It is now blocked by empty GameObjects containing just a BoxCollider2D. I managed to make it move correctly with high Linear Drag, however it still feels a bit too “physics”-y. Isn’t there a tried-and-true method of achieving old-school 2D 8-bit movement, with no acceleration or other funky stuff?
EDIT: In case someone searches for this, my simple solution might be useful (I had a hard time making this work or finding a simple example). BoxCollider2D + Rigibody2D on Player, Gravity Scale 0, Linear Drag 20. Empty GameObject with just a BoxCollider2D, adjusting size to your needs. On the Player, just add this:
void FixedUpdate() {
float x = Input.GetAxis ("Horizontal") * 50;
float y = Input.GetAxis ("Vertical") * 50;
gameObject.GetComponent<Rigidbody2D> ().AddForce(new Vector3 (x, y, 0));
}
Glad you’re at least a bit closer to solving your problem. You could try setting the rigidbody velocity directly? It isn’t perfect, but will help since you can apply a vector directly to it like transform.position and since it’s using rigidbody physics, it won’t clip through (as much). Another thing you can do is set up a collision system that ignores input when pressed against a wall (a raycast left or right would cover that, just disable right input when hitting right wall etc.). Finally, you could always use the CharacterController with the built in Move function. This will just slide the character against the wall when it hits it.
Again, hopefully I’m not telling you stuff you already know.
No, this is very helpful. The reason I’m trying to keep it simple is because I’m trying to teach Unity to my kid who has a good handling of programming and variables in Scratch. Some of the very simple things he can do in Scratch are quite more complex in Unity, so I try to avoid stuff such as raycasting… Thanks again!