Hello all! I just want to do a quick post regarding Godot 4, something I've been working with for awhile and absolutely LOVE.
If you don't know what Godot (pronounced guh-doh)is, it's an open source game engine that's almost ready for its 4.0 release.
Anyway, let's move on. One issue I had in my upcoming video game, Retro Hockey, is that I wanted a player to pass the puck to another player. I wanted them to have a "line of sight" (see below) for their passing in the shape of a big inverted triangle.
Then, if there's another player on the same team inside this triangle, and they are closer than another player, it would pass to the closer player.
Anyway, the problem I was having was I needed to figure out some way to rotate the triangle to the direction (Vector2
) the player was moving to.
The solution? the look_at
function!
In Godot 4, the look_at
function does this (official docs):
void look_at ( Vector2 point )
Rotates the node so it points towards the point, which is expected to use global coordinates.
Great! So now, let's just take the existing linear_velocity
of my character/player. Based on how your character moves, it may be different (something to keep in mind is how Godot and game programming uses the Cartesian plane), but here's the example I have:
# based on my char's movements, we reverse the x and y coords
var newVec = Vector2(linear_velocity.y, linear_velocity.x)
# set the look_at point to the initial global position of our triangle origin + the new vector. We also have to account for the y coord being negative since Godot/game programming uses the plane a bit differently...
passing_triangle.look_at(passing_triangle.global_position + (Vector2(newVec.x, -newVec.y)))
So then, with the View Collision Shapes
option on, we can see how this works! FYI, if you're not sure how to do this, here's a handy image for you:
And here's the final product! A giant triangle that rotates to where the player is going:
This post was heavily inspired by this old Reddit thread on Godot! Thank you for the help, stranger!
Top comments (1)
Hi, is there reason why are you using Area2D and not CharacterBody2D with physic?
docs.godotengine.org/en/4.0/tutori...