Teleporting a node an amount of units in a direction

Godot Version

Godot 4.2.2

Question

Hi! I’m very new to Godot, with a little bit of coding experience. I’m making a sample game to try out the engine. In the current version of the game, I have a player that can move (not much, I know…) and a weapon that does nothing right now. What I want is for the weapon to be a set distance from the player in the mouse cursor’s direction. Currently, I can get the weapon to point towards the mouse cursor but there is no “Move __ pixels in the direction you’re looking in” command that I can enter into the code.

(Current code if it helps)

(Concept art of what I want. P for player, W for weapon, C for cursor.)

Hi,

An easy way of doing that is to add a node for the weapon that’s a child node of the player. That way, you could rotate the weapon independently from the player.
Once that works, you can add a third node in between to act as the rotation pivot of the weapon, that will allow you to place the weapon at any distance, since the rotated object will be its parent.

So the nodes setup will look something like this:

Player
|_ WeaponRotation
   |_ Weapon

Another way of doing that is to calculate everything in code, which is not that hard, but I like the nodes solution better as it’s perfect to modify the weapon distance visually.
That’s the technique I’m using for my current project and it works like a charm.

aim

Let me know if that helps or if you need a more detailed explanation.

1 Like

I like the idea but I don’t think I was clear enough in my description. Part of my idea for the weapon is that it launches forward when used, and has a hitbox. The main part being that it launches forward. Does having it as a child node prevent that?

Actually that sounds like a perfect fit to me. By using a pivot, you can move your weapon forward by changing its local position on the x axis.

Say that the white square is the player, and the red square the weapon:



By changing the WeaponPivot node rotation, you can change the aim (as explained previously but I’ll still add a gif for the sake of giving a full breakdown of my idea):

rotation


By changing the Weapon local x position, you adjust the distance:

dist


And the cool thing is, you don’t have to do anything else for both the rotation and the weapon forward launching to work:

all

You can then work on a code that will do the launch motion the way you want, you can also do a back and forth motion, etc.
I hope I’ve understood your problem, feel free to tell me if that still doesn’t work for what you’re trying to do.

2 Likes

That should do it! Thank you. If I have any more issues I’ll come back on here and try to get them fixed.

1 Like