示例#1
0
    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();
    }