- Home /
Question by
FloppyGaming · Nov 08, 2019 at 09:04 AM ·
2dmovementcollision detectiontopdown
Tile based movement collisions
Inside my game I have tile-based movement, but I don't really know how to implement collisions. I currently have this code:
void OnTriggerEnter2D(Collider2D other) {
collidingObjects.Add(other.transform);
}
void Update() {
bool canGoUp = collidingObjects.Find(tf => tf.position == transform.position + Vector3.up) == null;
bool canGoRight = ...
if (Input.GetAxisRaw("Vertical") > 0 && movement.x == transform.position.x) {
if (canGoUp) movement.y++;
} else
// Other movements
}
This code works fine with object that are 1 tile big, but with objects that 2*2 it doesn't work. Let alone with objects with a weird shape, such as an L-shape. Is there a better way to check if the player can move in a certain direction?
Comment