#I need help
1 messages · Page 1 of 1 (latest)
i dont know how to code that
So you want the look_at() function to get your player to look in the direction of your mouse in 2d space
for this youll need the position of the mouse, which you can get from Input, try checking the docs for that
you can get the direction between two vectors by doing mouse_position - player_position
And when you click or whatever, spawn a bullet projectile into the scene (or if you want hit scan, instead send a ray cast and add vfx later)
how can i make the gun point to the mouse???
Look_at() function
yeah but like how can it look at the pointer
Yep. You will want to have your nodes probably setup like this:
Player -> GunHolder -> GunSprite
Then you have the GunSprite offset to look like the character is holding the gun properly. Finally, you make the Holder node look_at the mouse position 🙂 you will need to investigate the gun sprite a bit more tho (I’ll leave that for you to find out)
the look_at() function, as mentioned above
Let me pull up the doc
if i put the look_at() does it point to the mouse?
On the player, in your player script, you can get the current mouse position
You give that to that func
you have to tell it what to look at, its a parameter
yeah thats what im asking what do i put in the ()
so like, look_at(mouse_position)
oh ok thx
Here’s the documentation https://docs.godotengine.org/en/stable/tutorials/2d/2d_movement.html
Introduction: Every beginner has been there: "How do I move my character?" Depending on the style of game you're making, you may have special requirements, but in general the movement in most 2D ga...
To get the Vector2 position of the mouse, use get_global_mouse_position()
_process works
no problem, best of luck :)
if youre in a script thats attached to the player node, its just position, or self.position
im not
Otherwise you need a reference to the player object
what is your script attached to
And then access the global_position field
the gun node
is your gun node a child of your player?
no
You can just make the gun a child of player so it follows the player
ok
then if you do that i would recommend putting a script on the player, rather than the gun
i get this error
when you do position + 18, what is your goal?
offset it by 18
in what direction
the direction of the mouse
hmmm
others can correct me if im wrong, but you might be able to just move it in the positive X direction, since it is already rotated towards the mouse?
yeah thats what im tryna do
for which you can do self.global_position.x += 18
oh ok
The issue is you can only add a vector2 value with another vector2 value
if you do this every frame though, your gun will "run away" so to speak
k i changed it to this now tho so i dont get the error
oh ok
youll need to have it do that offset based on some anchor position
like the player's position
I personally would just offset the gun relative to the player in the editor instead of code to avoid any hassle
yeah, agreed
yeah
definitely recommend this setup
This is how I want it to move srry for the quality I recorded on my phone
yup, if you follow the setup above you should be able to get this effect
this one?
yup :)
what does he mean gunholder?
just a blank Node2D named gunholder
ok
it can be named anything, renaming it just helps with organizing
yeah
if you make a node a child of another node, it will inherit its parent's position and rotation
so if you rotate the GunHolder, the gun will rotate with it
how can i move it like in the vid tho?
just put a script on GunHolder that rotates itself toward the mouse, like you had it before
yeah i have that
you can probably discard the script on the gun
whats it doing now
but it rotates it doesnt move
is the gun at the same position as the GunHolder?
if so, move it to the right some amount
it just flies off
are you using the same script as before?
this right?
is the script on Gun or GunHolder
the latter
no
they should
ik the problem i think
you need to move the gun out, not the GunHolder
this just looks at the mouse
yup, thats what you want i think
wait lemme see
the setup were suggested is letting GunHolder be the "pivot" that will rotate your gun
so youll need the GunHolder and the player to have the same position, and for the gun sprite to be offset by some amount
OH YEAH IT WORKS
FINNALLY
you dont know how long ive tried to make this thank you
glad i could help :)
it should if GunHolder is a child of the player
huh..
does GunHolder have "top level" checked in the visibility section or something?
no but i checked it and it dissapeared
well you dont want that checked, i was just making sure
oh ok
youre not setting the GunHolder's global position anywhere?
no
weird..
without knowing more about your setup im not sure why it wouldnt be following
they are fine being diffrent scenes right?
should be fine?
ok anuwau i gtg
anyway
cya
wait
can i friend you so if i have another question i can dm u?
id prefer not to add people i dont know, but you'd always be free to ask in this server again