–1 vote

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 physicsprocess(delta):
if Input.isactionpressed("Shoot"):
shoot()

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

Godot version 3.3.2
in Engine by (28 points)

2 Answers

+2 votes
Best answer

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.

by (136 points)
selected by

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

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

0 votes

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?

by (136 points)
edited by

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

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

video : https://we.tl/t-PYO9CssdrF
video 2 : https://www.youtube.com/watch?v=dQw4w9WgXcQ

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?

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 physicsprocess(delta):
if Input.isactionpressed("uiright"):
motion.x = speed
elif Input.is
actionpressed("uileft"):
motion.x = mspeed
elif Input.isactionpressed("uiup"):
motion.y = mspeed
elif Input.is
actionpressed("uidown"):
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 :
https://we.tl/t-y9hZvLtrSW

just a question,du you live in london ?

(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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.