why my object don't go to the same position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lgdn

hello

i have an object (bullet) that when i press shoot that go in the position2d named “shoot”
but the bullet don’t go to the same position
the rigidbody2D is the gun where is the positon2d

how to fix that

extends RigidBody2D

var bulletPath = preload(“res://Bullet.tscn”)

func _physics_process(delta):
if Input.is_action_pressed(“Shoot”):
shoot()

func shoot():
var bullet = bulletPath.instance()
print(“ok”)
get_parent().add_child(bullet)
bullet.position = $Shoot.global_position

:bust_in_silhouette: Reply From: Montasir Raihan

Hello.

Bit of advise, use the “Code Sample” button to format your code when you post your questions. It’ll make your code much more readable.

However, I don’t see any errors in your code. I’ve tested it on my own Godot engine. The code looks like this.

extends RigidBody2D

var bulletPath = preload("res://Bullet.tscn")

func _physics_process(delta):
   if Input.is_action_pressed("Shoot"):
      shoot()

func shoot():
    var bullet = bulletPath.instance()
    print("ok")
    get_parent.add_child(bullet)
    bullet.position = $Shoot.global_position

Everything works perfectly. Are you getting any errors in the debugger?

And yes. I also need to see your node structure.

Montasir Raihan | 2021-07-23 18:56

that don’t work corectly , has you can see in this video the bullet is not appear in the gun

video : WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free
video 2 : https://www.youtube.com/watch?v=dQw4w9WgXcQ

Lgdn | 2021-07-23 22:21

Nope. Nothing’s wrong in your script file Gun.gd. Everything’s okay. Can I see the script of Player.gd and the node structure of your Player scene?

Montasir Raihan | 2021-07-24 02:36

sure but have you watch the second video ?

there the code here :
extends KinematicBody2D

const UP = Vector2(0, -1)
var motion = Vector2()
var speed = 200
var mspeed = -200

func _physics_process(delta):
if Input.is_action_pressed(“ui_right”):
motion.x = speed
elif Input.is_action_pressed(“ui_left”):
motion.x = mspeed
elif Input.is_action_pressed(“ui_up”):
motion.y = mspeed
elif Input.is_action_pressed(“ui_down”):
motion.y = speed
else:
motion.x = 0
motion.y = 0

    
    motion = move_and_slide(motion,UP)
pass


if Input.is_action_pressed("Slowmo"):
	Engine.time_scale = 0.1
else :
	Engine.time_scale = 1

	
if Input.is_action_pressed("Reset"):
	get_tree().reload_current_scene()

and
there
is
the
IMAGE :
WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

just a question,du you live in london ?

Lgdn | 2021-07-24 10:18

(just a question,du you live in london ?) → No I don’t. Why the question?
And yes I’ve seen your 2nd video. Rick Astley’s song Never Gonna Give You Up. I don’t know why you’ve sent me this link. But I’m trying to solve your problem. I need to focus on that. Soon I’ll give you an answer.

And besides I’ve answered your question '‘Why my game freeze?’. Did you check it?

Montasir Raihan | 2021-07-24 11:03

:bust_in_silhouette: Reply From: Montasir Raihan

Now I know where is the problem. I want you read this attentively.

The Gun node is the direct child of Ragdoll node. And the Ragdoll node is the direct child of Player node. “get_parent().add_child(bullet)” spawns a bullet scene as a child of Ragdoll node. That’s why the position of your bullet is messed up. Don’t worry. You just have to change the code to solve the issue.

Go to your script file Gun.gd. Write the following code.

func shoot():
    var bullet = bulletPath.instance()
    print("ok")
    # Get the root node to spawn bullets
    # Cause if the bullets don't spawn in the root node
    # The bullets will move and rotate when you move your player
    # And bullets will spawn in the wrong position
    get_tree().root.add_child(bullet)
    bullet.position = $Shoot.global_position

I’ve tested the result. It works. Try it. And let me know if this works for you too. I hope it will.

Yes ! that work correctly,thank you and have an nice day :slight_smile:

Lgdn | 2021-07-25 09:54

I’ve answered your question '‘Why my game freeze?’. Did you check it?

Montasir Raihan | 2021-07-25 12:02