your constant linear velocity should be a vector from your spawn position to your destination position ( = destPos - spawnPos).
I think that’s your problem
Normalized vectors are your friend here. If you haven’t run into them, a normalized vector is a vector whose total length is 1.0. Godot has functions to produce them. Once you have a normalized vector, you can do all sorts of things with it.
You can get a normalized vector from a position to a target:
var norm_vec = (target - position).normalized()
You can scale that by the speed of the arrow:
var arrow_vec = norm_vec * ARROW_SPEED
You can determine the angle the arrow should be facing:
var angle = norm_vec.angle()
My suspicion is that your “shoots itself” problem looks like you’re pulling the launch position from the animation, and the animation may be lagging. I think you probably want to drive the animation off the shooting, not the reverse.
You could use your normalized vector multiplied by a fixed radius to choose the launch position:
Thank you for your answer! I think I have a poor understanding of what I wrote.
How I imagined the shot:
When the mouse clicks on any part of the viewport, unhandled_input is called, which stores the click coordinates via get_global_mouse_poistion
An Arrow object is created, into which the mouse click coordinates are passed
_ready() for Arrow, having received the mouse coordinates, sets the starting position (global_position) and calculates global_rotation
Then hitTimer starts, which in my case starts the attackDelay process, during which the animation is played
When the timer ends, the previously created Arrow is sent to add_child of the player’s parent (i.e. the map/level).
the arrow with the calculated linear_velocity flies to the previously stored mouse click coordinates - profit
But I get some kind of nonsense)))
The problem I don’t understand is not hitting myself, but shifting everything that is on the x coordinate plus or minus depending on where the shooter is now. Next - the center of the circle will be the end point of my collision-radius of the arrow or to the right. If the current x coordinates of the shooter > 0, then the center point of the circle (aka the spawn position of the arrow) will shift to the right, if x < 0 then the shift will be to the left. If the x coordinate == 0 then everything works as I would like ;D.
I think this code which calculate startPos for arrow is wrong.
func getPointInsideCircleRadiusFromDirection(position: Vector2, radius: float, direction: Vector2) -> Vector2:
var radians: float = direction.angle()
var cos: float = cos(radians)
var sin: float = sin(radians)
var vect: Vector2 = Vector2(position.x + radius * cos, position.y + radius * sin)
return vect
And I would be very grateful if you could help me with some advice on this piece of code I wrote.
I think the problem might be that you’re creating the arrow, then waiting for the timer, then launching it. If anything changes while the clock is running, your arrow is no longer where it’s supposed to be.
You could still create them before, but maybe redo the position/direction math at the moment of launch to make sure they’re where they’re supposed to be. If you do that and see them snapping around between creation and launch, that’ll be what’s going on.