#I need help

1 messages · Page 1 of 1 (latest)

quaint oasis
#

what part of this are you struggling with

signal hedge
#

i dont know how to code that

frank roost
#

So you want the look_at() function to get your player to look in the direction of your mouse in 2d space

quaint oasis
#

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

frank roost
#

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)

signal hedge
#

how can i make the gun point to the mouse???

frank roost
#

Look_at() function

signal hedge
#

yeah but like how can it look at the pointer

serene lark
#

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)

quaint oasis
#

the look_at() function, as mentioned above

frank roost
#

Let me pull up the doc

signal hedge
#

if i put the look_at() does it point to the mouse?

serene lark
#

On the player, in your player script, you can get the current mouse position

#

You give that to that func

quaint oasis
#

you have to tell it what to look at, its a parameter

signal hedge
quaint oasis
#

so like, look_at(mouse_position)

signal hedge
#

oh ok thx

frank roost
#

To get the Vector2 position of the mouse, use get_global_mouse_position()

signal hedge
#

on a _ready func?

#

or a process?

quaint oasis
#

_process works

signal hedge
#

k

#

ok i think i got it if i need any more help ill tell u guys thx for the help

quaint oasis
#

no problem, best of luck :)

signal hedge
#

thx

#

how can i get the player position

quaint oasis
#

if youre in a script thats attached to the player node, its just position, or self.position

signal hedge
#

im not

frank roost
#

Otherwise you need a reference to the player object

quaint oasis
#

what is your script attached to

frank roost
#

And then access the global_position field

signal hedge
#

the gun node

quaint oasis
#

is your gun node a child of your player?

signal hedge
#

no

frank roost
#

You can just make the gun a child of player so it follows the player

signal hedge
#

ok

quaint oasis
#

then if you do that i would recommend putting a script on the player, rather than the gun

signal hedge
#

i get this error

quaint oasis
#

when you do position + 18, what is your goal?

signal hedge
#

offset it by 18

quaint oasis
#

in what direction

signal hedge
#

the direction of the mouse

quaint oasis
#

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?

signal hedge
#

yeah thats what im tryna do

quaint oasis
#

for which you can do self.global_position.x += 18

signal hedge
#

oh ok

frank roost
quaint oasis
signal hedge
#

oh ok

quaint oasis
#

youll need to have it do that offset based on some anchor position

#

like the player's position

frank roost
#

I personally would just offset the gun relative to the player in the editor instead of code to avoid any hassle

quaint oasis
#

yeah, agreed

signal hedge
#

yeah

quaint oasis
signal hedge
quaint oasis
#

yup, if you follow the setup above you should be able to get this effect

quaint oasis
#

yup :)

signal hedge
#

what does he mean gunholder?

quaint oasis
#

just a blank Node2D named gunholder

signal hedge
#

ok

quaint oasis
#

it can be named anything, renaming it just helps with organizing

signal hedge
#

yeah

quaint oasis
#

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

signal hedge
#

how can i move it like in the vid tho?

quaint oasis
#

just put a script on GunHolder that rotates itself toward the mouse, like you had it before

signal hedge
#

yeah i have that

quaint oasis
#

you can probably discard the script on the gun

quaint oasis
signal hedge
#

but it rotates it doesnt move

quaint oasis
#

is the gun at the same position as the GunHolder?

#

if so, move it to the right some amount

signal hedge
#

it just flies off

quaint oasis
#

are you using the same script as before?

signal hedge
#

this right?

quaint oasis
#

ah right

#

if this is on the GunHolder and not the Gun, just remove line 6

signal hedge
#

yeah now it just looks at the gun

#

the mouse srry

quaint oasis
#

can you take a screenshot?

#

or do you mean that its working

signal hedge
#

imma send a vid

#

wait

#

dont mind the eminem music im listening to it

quaint oasis
#

is the script on Gun or GunHolder

signal hedge
#

the latter

quaint oasis
#

hm

#

do the player and the GunHolder have the same position?

signal hedge
#

no

quaint oasis
#

they should

signal hedge
#

ik the problem i think

quaint oasis
#

you need to move the gun out, not the GunHolder

signal hedge
#

this just looks at the mouse

quaint oasis
#

yup, thats what you want i think

signal hedge
#

wait lemme see

quaint oasis
#

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

signal hedge
#

OH YEAH IT WORKS

#

FINNALLY

#

you dont know how long ive tried to make this thank you

quaint oasis
#

glad i could help :)

signal hedge
#

tysm

#

oh wait

#

it doesnt follow the player

quaint oasis
#

it should if GunHolder is a child of the player

signal hedge
#

it is

quaint oasis
#

huh..

#

does GunHolder have "top level" checked in the visibility section or something?

signal hedge
#

no but i checked it and it dissapeared

quaint oasis
#

well you dont want that checked, i was just making sure

signal hedge
#

oh ok

quaint oasis
#

youre not setting the GunHolder's global position anywhere?

signal hedge
#

no

quaint oasis
#

weird..

#

without knowing more about your setup im not sure why it wouldnt be following

signal hedge
#

they are fine being diffrent scenes right?

quaint oasis
#

should be fine?

signal hedge
#

ok anuwau i gtg

#

anyway

#

cya

#

wait

#

can i friend you so if i have another question i can dm u?

quaint oasis
#

id prefer not to add people i dont know, but you'd always be free to ask in this server again

signal hedge
#

ok

#

understood