0 votes

Hello!

Currently, my project allows for movement and firing of pellets. However, as it stands now, these pellets/lasers are spawned in at the correct location, but they follow the ship around. So I guess this was two questions, 1) how to get pellets/lasers to move once spawned (linearly), and 2) how to 'detach' the pellets from the ship.

Thanks for any kind of help.

And the "pellet.xml" scene is empty in terms of scripts. It only has the pellet texture placed within the scene with an offset of -48.
Code:

extends KinematicBody2D

#Preloading and/or pellet/ship variables
var pellet = preload("res://pellet.xml")
var pelletCount = 0

#Horizontal variables
var input_directionx = 0
var directionx = 0

var speedx = 0
var velocityx = 0

#Vertical variables
var speedy = 0
var velocityy = 0

var input_directiony = 0
var directiony = 0

#Rotational variables
var turn_speed = 200
var turnd = 0

#Constants
const MAX_SPEED = 600
const ACCELERATION = 2000
const DECELERATION = 1000

func _ready():
set_process(true)
pass

func _process(delta):
if input_directionx:
    directionx = input_directionx

if input_directiony:
    directiony = input_directiony
#Input X
if Input.is_action_pressed("A"):
    input_directionx = -1
elif Input.is_action_pressed("D"):
    input_directionx = 1
else:
    input_directionx = 0

#Input Y
if Input.is_action_pressed("W"):
    input_directiony = -1
elif Input.is_action_pressed("S"):
    input_directiony = 1
else:
    input_directiony = 0

#Input and rotation
if Input.is_action_pressed("LSHIFT"):
    turnd = get_rotd() + turn_speed * delta
    set_rotd(turnd)

if Input.is_action_pressed("SPACE"):
    turnd = get_rotd() + turn_speed * delta * -1
    set_rotd(turnd)

if Input.is_action_pressed("Q"):
    fire()

#Input X2
if input_directionx:
    speedx += ACCELERATION * delta
else:
    speedx -= DECELERATION * delta
speedx = clamp(speedx, 0, MAX_SPEED)

#Input Y2
if input_directiony:
    speedy += ACCELERATION * delta
else:
    speedy -= DECELERATION * delta
speedy = clamp(speedy, 0, MAX_SPEED)

velocityx = speedx * delta * directionx
velocityy = speedy * delta * directiony

move(Vector2(velocityx, 0))
move(Vector2(0, velocityy))

pass

func fire():
var pellet_instance = pellet.instance()
pellet_instance.set_name("Pellet" + str(pelletCount))
#print(pellet_instance.get_name())
add_child(pellet_instance)
var pelletPos = get_node("Pellet" + str(pelletCount)).get_pos()
print("pelletPos")
print(pelletPos)
print("pellet name = " + pellet_instance.get_name())
var pelletPos = pellet_instance.get_pos()

var p2Pos = get_node("Player2Model").get_pos()
print("Player1Model pos")
print(p2Pos)
pelletPos.y = p2Pos.y
pelletPos.x = p2Pos.x
get_node("Pellet" + str(pelletCount)).set_pos(pelletPos)
pelletCount += 1
print("Fire!")
in Engine by (15 points)

I made a little plugin that uses RigidBodies for bullets, you can find it on the asset store or just look over the code on GitHub - https://github.com/JarLowrey/GodotGuns . As @YeOldeDM says, you probably want to add the bullets as children to the root node instead of the gun/shooter node get_node("/root").add_child(bullet)

Thanks jarlowrey....this golden piece of code...i needed that.

Yay someone used it! Happy to help :)

1 Answer

+1 vote

In order to get your bullets to move on their own, they should really have their own script with movement code done under _fixed_process. You may want to make your bullet an Area2D, along with a collisionshape (and the Sprite of course). Doing this will allow the bullet to detect when it collides with something; which might be something you want to have at some point.

When you add a child node to its parent, its position will be relative to that parent. You don't want the bullet to move relative to the ship, but the world that acts as the ship's parent. So rather than add_child( pellet ), you could use get_parent().add_child( pellet ). When you do that though, you will need to set the bullet's starting position to be relative to the ship's origin.

by (1,330 points)
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 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 webmaster@godotengine.org with your username.