private void HandleCollision(Collision2D collision) { Vector2 moveDirection; // If the ice collided with the player, the ice should move away from the player if (collision.gameObject.CompareTag("Player") || collision.gameObject.CompareTag("Ice")) { Direction2D hitFromDir = collision.GetDirectionHitCameFrom(); moveDirection = hitFromDir.Vector; } // If the ice collided with something else like a wall else { Direction2D hitFromDir = collision.GetDirectionHitCameFrom(); //Direction2D curMoveDirection = parentPhys.velocity.GetDirection2D(); // If the direction changed to the opposite direction we were previously moving in, // the ice cube probably had a direct collision and should stop moving if (hitFromDir.IsOppositeDirection(previousDirection)) { moveDirection = Vector2.zero; } // If the direction only changed slightly and is not in the opposite direction, // the ice cube probably just brushed against something and should keep moving as it was else { moveDirection = previousDirection.Vector; } } // Set the ice's velocity to be fully in its movement direction parentPhys.velocity = moveDirection * slideSpeed; // Update the previous direction previousDirection = parentPhys.velocity.ToDirection2D(); }
public static QuadDirection2D Add(this Direction2D curDir, Direction2D otherDir) { // If current direction is none if (curDir == Direction2D.none) { return(otherDir.ToQuadDirection2D()); } // If other direction is none if (otherDir == Direction2D.none) { return(curDir.ToQuadDirection2D()); } // Neither direction is none // If they are the same direction if (curDir == otherDir) { return(curDir.ToQuadDirection2D()); } // If they are opposite directions if (curDir.IsOppositeDirection(otherDir)) { return(QuadDirection2D.none); } // They are orthogonal // Left if (curDir == Direction2D.left) { if (otherDir == Direction2D.up) { return(QuadDirection2D.upLeft); } if (otherDir == Direction2D.down) { return(QuadDirection2D.downLeft); } } // Right if (curDir == Direction2D.right) { if (otherDir == Direction2D.up) { return(QuadDirection2D.upRight); } if (otherDir == Direction2D.down) { return(QuadDirection2D.downRight); } } // Up if (curDir == Direction2D.up) { if (otherDir == Direction2D.left) { return(QuadDirection2D.upLeft); } if (otherDir == Direction2D.right) { return(QuadDirection2D.upRight); } } // Down if (curDir == Direction2D.down) { if (otherDir == Direction2D.left) { return(QuadDirection2D.downLeft); } if (otherDir == Direction2D.right) { return(QuadDirection2D.downRight); } } Debug.LogError("Unhandled conditions in Direction2D"); return(QuadDirection2D.none); }