Reputation: 21
I worked on a small fishing game in Godot for a Game Jam and as it was my first time creating something in Godot, for much of it I followed the Your first game tutorial from the Godot docs. In this tutorial, mobs are spawned from all sides of the screen, but for my game I wanted the fish to spawn only from the left and right side. Because of this change, instead of using one Path2d for the mob spawning, I used two straight lines on either side.
The lines were placed with the points in a clockwise order, so on the right side, the first point was the top and the second was the bottom, and on the left side, the first point was the bottom and the second was the bottom. Here is a picture that shows the spawning lines.
The mobs spawn and move in the correct direction, but no matter what I tried, the fish that spawned from the right side spawned with an incorrectly placed hitbox and with the sprite upside-down. This is an example of a fish that spawned from the left side, while this is an example of a fish that spawned from the right side.
Here is the code for spawning fish. Side is a variable that passed in and is simply 0 if the fish is spawning from the left and 1 if the fish is spawning from the right.
var fish = Fish.instance()
add_child(fish)
var fishSide
if (side == 0):
fishSide = $FishPathLeft/FishFollowLeft
else:
fishSide = $FishPathRight/FishFollowRight
fishSide.offset = randi()
fish.linear_velocity = Vector2(fish.speed, 0)
var direction = fishSide.rotation + PI / 2
fish.rotation = direction
fish.position = fishSide.position
fish.linear_velocity = fish.linear_velocity.rotated(direction)
When a fish is created, they are given a random base sprite from 4 possible bases, all of which have specific settings for the collision shape. The fish sprites default to facing left.
I assumed that the issue was because the fish is rotated 180 degrees when spawned on the right, but the problem is that even if I try to flip the fish, it still appears upside-down. I've tried flipping the fish with:
fish.scale.y = -fish.scale.y
But it doesn't seem to do anything, so I'm kind of at a loss as to what would fix it.
Upvotes: 2
Views: 771
Reputation: 40220
What I see here is that you are rotating the fish. Which also explains the pictures:
fish.rotation = direction
But that is not what you want.
Yes, you could use scale (fish.scale.y = -fish.scale.y
), However, it is not recommended to scale them.
The sprite has flip_v
and flip_h
properties, that will solve the visual part. But they have nothing to do with the collision shape.
So you could do this:
fish.get_node("sprite").flip_h = (side == 1)
Where sprite
is a sprite child of the fish.
What I'm going to suggest for the collision shape is to have two, one for the left and one for the right, and the disable the one the fish will not use (shape.disabled = true
).
You could do something like this:
fish.get_node("left_collision").disabled = (side == 1)
fish.get_node("right_collision").disabled = (side == 0)
Where left_collision
and right_collision
are collision shapes children of the fish.
As an alternative, consider rotating the collision shape a quarter turn clockwise (-TAU / 4
, a.k.a -PI / 2
):
fish.get_node("collision").rotation = -TAU / 4 if (side == 1) else 0
By the way, in the long run you might benefit from placing all this logic in a function in a script attached to the root node of fish scene. Have the function take the velocity as input, and using it decide if it is going to flip and rotate the sprite, and if it is going to disable or rotate collision shapes. That way, if you later decide you want the fish to be able to change direction, it is a matter of calling that function again (in fact, you could do that in _physics_process
of the fish).
Something else (I know you do it the way the tutorial does it, but): give position the node before adding it as child. If you add it first, and place it second, it might register some collisions before you change the position.
Upvotes: 1