#Instanced projectile firing toward mouse position is slight offset. 2D TOPDOWN

96 messages · Page 1 of 1 (latest)

left dagger
#

Hello,

I have successfully made this eye shoot bullets, but they are slight offset, ChatGDP summarized the issue pretty well.

The slight offset you're experiencing might be due to the difference between the gun's rotation and the direction of the mouse cursor. When you set the rotation of the bullet instance to match the gun's rotation, it doesn't necessarily mean the bullet will move in the same direction as the mouse cursor.

Sadly, most of the help it gives is Godot 3.X and not fully up to date.
Here is the code from the player :

    if Input.is_action_just_pressed("fire"):
        shoot()
        
        
func shoot():
    
    var mousepos = get_viewport().get_mouse_position()
    var bullet_instance = bullet.instantiate()
    
    gun.look_at(mousepos)
    owner.add_child(bullet_instance)
    
    bullet_instance.global_position = gun.global_position
    bullet_instance.global_rotation = gun.global_rotation```

BULLET CODE :

```extends Area2D
var speed = 150
var direction = 0
# Called when the node enters the scene tree for the first time.
func _physics_process(delta):
    position += Vector2.from_angle(direction) * speed * delta    

S```

Find attached, a GIF of the issue.
Thanks !
vast knoll
#

chatgpt is pretty much useless for learning godot 4. free chatgpt 3.5, at least
it produces a lot of correct lines but makes way too much errors in between for a beginner to realize

we have great tutorial videos on youtube for almost any genre, i recommend checking those out since you can not only see some good code but also how to set it all up correctly in the editor

left dagger
#

oh yes, i am aware, it seems to run on a jan. 2022 database

#

i use it to ask wide questions, but never or rarely code itself since the syntax is outdated

#

i follow courses and tutorials on the side gdpicardia

vast knoll
#

alright, thats a good approach!

so theres a direction var in your bullet code but it isnt set anywhere, as far as i can tell. and you dont need it really.
the direction it should travel is already stored in its transform, since you assigned its rotation to the gun rotation.
position+= global_transform.x * speed * delta
would be the easier way to do this

#

adding the bullet to the owner might also be the wrong idea here, depending on who the owner is

#

i think its generally safer to add the bullet as a child to the root scene node get_tree().current_scene.add_child()

#

because if the owner of the bullet was the player, for example, it would still move and rotate with the player

left dagger
left dagger
fresh flint
#

you want it to keep firing in a straight line ?

#

and not have any offset

left dagger
#

yes!

fresh flint
#

the reason for the offset is your transform is changing constatly

left dagger
#

by rotating the gun, it changes it's transform ? gdsweat

fresh flint
#

yes

#

you are rotating it

#

ik a fix

#

try set the velocity of the bullet at the moment of instantiation

left dagger
#

ah, so i guess using look_at toward the mouse cursor wasn't a safe thing to do

fresh flint
#

i am just shooting out there but try

func shoot():
    var mousepos = get_viewport().get_mouse_position()
    var bullet_instance = bullet.instantiate()
    
    gun.look_at(mousepos)
    owner.add_child(bullet_instance)
    
    bullet_instance.global_position = gun.global_position
    bullet_instance.velocity = (mousepos -gun.global_position).normalized() * speed

bullet code in physics process

position += velocity * speed * delta
left dagger
#

trying

vast knoll
# left dagger thank you for the explanation though, i though owner and get_tree().current_scen...

owner will refer to the most immediate scene parent. since your player is a scene, in this case if the script runs from the player node or any of its children it will refer to the player scene root node
btw, its common practice to have the physics body be the root node of your scene. so the player scene should have the CharacterBody as root

godot expects the default rotation to be facing right in 2d. if your gun sprite faces to the right look_at() should work in theory

left dagger
#

i haven't been able to apply your logic right now, i think there was an issue that my bullet tree has a parent on top of it, and right now that parent being a Node2D is receiving the logic and it doesn't like it. (Invalid set index 'velocity' (on base: 'Node2D (bulletmain.gd)') with value of type 'Vector2'.)
working on fixing that.

left dagger
vast knoll
#

at least, for Areas it isnt

left dagger
#

even less for a base Node2D i assume

vast knoll
#

btw, the Area should be the root node of this scene here

left dagger
#

yes, changing

fresh flint
#

new info

#

but wait

#

area2d's dont have a velocity property either

vast knoll
#

only CharacterBody has a built-in velocityproperty

#

Rigidbodies have linear_velocity

fresh flint
#

yea

#

use linear velocity for that one

left dagger
#

tree is fixed, game isn't crashing, logic got applied on the bullet area2D node, bullet's aren't moving and despawning instantly (timer isn't the culprit)

fresh flint
#

yea show the code

#

for the bullet

left dagger
#

var speed = 150
var velocity = 200

## Called when the node enters the scene tree for the first time.
func _physics_process(delta):
    position += velocity * speed * delta


func _on_timer_2_timeout():
    queue_free()```
#

player logic below

    if Input.is_action_pressed("fire"):
        shoot()
        
        
func shoot():
    
    var mousepos = get_viewport().get_mouse_position()
    var bullet_instance = bullet.instantiate()
    
    gun.look_at(mousepos)
    owner.add_child(bullet_instance)
    
    bullet_instance.global_position = gun.global_position
    bullet_instance.velocity = (mousepos -gun.global_position).normalized() * speed```
fresh flint
#

hmm

#

what if you define velocity as a vector2

#

var velocity = Vector2()

#

or use your old code

#

but then that had angle in it

#

like

#

Here's how you can modify your shoot() function:

func shoot():
    var mousepos = get_viewport().get_mouse_position()
    var bullet_instance = bullet.instantiate()
    
    gun.look_at(mousepos)
    owner.add_child(bullet_instance)
    
    bullet_instance.global_position = gun.global_position + Vector2(bullet_instance.texture.get_width() / 2, 0).rotated(gun.rotation)
    bullet_instance.direction = (mousepos - gun.global_position).angle()

And here's how you can modify your _physics_process(delta) function:

func _physics_process(delta):
    position += Vector2.from_angle(direction) * speed * delta
#

look_at() is the culprit

#

i think

left dagger
#

testing

#

Invalid get index 'texture' (on base: 'Area2D (Bullet.gd)').

fresh flint
#

oh right wait

#

you need to get the texture size

#

save it in a variable

#

because

left dagger
#

22x22

fresh flint
#

yeah

#

try typing it in manually

#

22 / 2, 0

#

which is 11

#

so 11, 0

#

bullet_instance.global_position = gun.global_position + Vector2(11, 0).rotated(gun.rotation)

left dagger
#

good news

#

it's shooting

fresh flint
#

yay!

left dagger
#

bad news

fresh flint
#

its not straight ?

left dagger
#

testing things

fresh flint
#

func shoot():
var mousepos = get_viewport().get_mouse_position()
var bullet_instance = bullet.instantiate()

bullet_instance.global_position = gun.global_position + Vector2(11, 0).rotated(gun.rotation)
bullet_instance.direction = (mousepos - gun.global_position).angle()
owner.add_child(bullet_instance)
left dagger
#

the weird thing, is that, even when it is centered (the gun / eye flies next to the player), it still has an offset

fresh flint
#

try that

left dagger
fresh flint
#

lmao

#

func shoot():
var mousepos = get_viewport().get_mouse_position()
var bullet_instance = bullet.instantiate()

bullet_instance.global_position = gun.global_position + Vector2(11, 0).rotated(gun.rotation)
bullet_instance.rotation = gun.rotation
bullet_instance.direction = (mousepos - gun.global_position).angle()
owner.add_child(bullet_instance)
#

try this

left dagger
#

exact same issue

fresh flint
#

hmm

#

then idk lol

#

u can use your old code

left dagger
#

at least i fixed my tree issue

#

thank you for your time! i'll find a solution eventually

left dagger
#

Ok, found the culprit

#

var mousepos = get_viewport().get_mouse_position()

#

i just had to change it to : var mousepos = get_global_mouse_position()

#

final code for people that will travel through here :

#

BULLET

var speed = 150
# Called when the node enters the scene tree for the first time.
func _physics_process(delta):
    position+= global_transform.x * speed * delta```
#

PLAYER/GUN

    if Input.is_action_pressed("fire"):
        shoot()
        
func shoot():
    
    var mousepos = get_global_mouse_position()
    var bullet_instance = bullet.instantiate()
    
    gun.look_at(mousepos)
    owner.add_child(bullet_instance)
    
    bullet_instance.global_position = gun.global_position
    bullet_instance.global_rotation = gun.global_rotation```
#

@fresh flint FYI!