Bullet spawning on top of characte!

VERSION: 4.3

Im trying to make like the main weapon of the character spawn from him and go in a straight line, it is going in a straight line but it keeps spawning on top of the character, Why? Im not sure…
but is definitely something i did.

Code inside projectile/Glove:

extends Area2D

var Speed = -450

func _process(delta):
	position.x -= Speed * delta
	pass

bullet code inside CharBody2D:

func shoot():
	var glove = Bulleto.instantiate()
	var horizontal_offset = -20
	var vertical_offset = -10
	
	bullet.position = sprite.position
	if sprite.flip_h:
		bullet.position = position + Vector2(-horizontal_offset, vertical_offset)
	else:
		bullet.position = position + Vector2(horizontal_offset, vertical_offset)
		
	get_parent().add_child(glove)

Hole code:

const SPEED = 500
const MOONWALK_SPEED = 750
const JUMP_VELOCITY = -560
const JUMP_MAX_HEIGHT = -760

const Heli_S = 150  
var is_gliding = false

@onready var sprite = $AnimatedSprite2D
@onready var rc = $RayCast2D
@onready var GRC = $GliderRC
@onready var Bulleto = preload("res://glove.tscn")

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var is_jumping = false

func _physics_process(delta):
	
	if not is_on_floor():
		if is_gliding:
			
			if velocity.y < Heli_S:
				velocity.y = Heli_S
		else:
			
			velocity.y += gravity * delta
	else:
		
		is_gliding = false

	
	if Input.get_action_strength("ui_accept") && is_on_floor():
		velocity.y = JUMP_VELOCITY
		is_jumping = true

	
	if Input.get_action_strength("high_jump") && is_on_floor():
		velocity.y = JUMP_MAX_HEIGHT
		is_jumping = true

	
	if Input.is_action_pressed("Helicopter_1") and not is_on_floor():
		is_gliding = true
	else:
		is_gliding = false
	
	
	if not is_on_floor() and is_gliding:
		if sprite.animation != "Helicopter":
			sprite.play("Helicopter")

	
	elif not is_on_floor() and not is_gliding:
		if sprite.animation != "jump":
			sprite.play("jump")

	
	var direction = Input.get_axis("ui_left", "ui_right")
	var is_running_right = Input.is_action_pressed("Running_Right")
	var is_running_left = Input.is_action_pressed("Running_Left")

	if is_running_right and is_on_floor():
		velocity.x = Runnin_VV
		sprite.play("Running")
		sprite.flip_h = false
	elif is_running_left and is_on_floor():
		velocity.x = -Runnin_VV
		sprite.play("Running")
		sprite.flip_h = true
	elif direction != 0:
		velocity.x = direction * SPEED
		if sprite.animation != "Walk" and is_on_floor():
			sprite.play("Walk")
	else:
		velocity.x = 0
		if is_on_floor() and sprite.animation != "idle":
			sprite.play("idle")

	
	if Input.is_action_pressed("moon_walk") and is_on_floor() and not is_running_left and not is_running_right:
		if direction != 0 and rc.is_colliding():
			sprite.play("Walk")
			velocity.x = -MOONWALK_SPEED * direction
		else:
			velocity.x = -MOONWALK_SPEED * sign(velocity.x)

	
	if Input.is_action_just_pressed("ui_left"):
		sprite.flip_h = true
	elif Input.is_action_just_pressed("ui_right"):
		sprite.flip_h = false

	move_and_slide()
	
	if Input.is_action_just_pressed("glove shot"):
		shoot()
		
func shoot():
	var bullet = Bulleto.instantiate()
	var horizontal_offset = -20
	var vertical_offset = -10
	
	bullet.position = sprite.position
	if sprite.flip_h:
		bullet.position = position + Vector2(-horizontal_offset, vertical_offset)
	else:
		bullet.position = position + Vector2(horizontal_offset, vertical_offset)
		
	get_parent().add_child(bullet)

Any Help is appreciated!

try this , make a position 2d node for your player and then:

bullet.position = $position2D.position

i hope this is useful for you

2 Likes

Hi thank you for reaching out

The Position2D Node name was changed to Marker2D
And i did as you asked but it didnt change.

Code:

I think your problem is that you set the position of the bullet to the $Marker2D.position, then in your if statement, you immediately set it to the position of the CharcterBody2D.

bullet.position = sprite.position <--- setting it here
if sprite.flip_h:
	bullet.position = position + ... <--- setting it again here
else:
	bullet.position = position + ... <--- or here

I would guess (without testing it myself) you need to prepend bullet. to both paths in your if/else to apply the offset. e.g.:

if sprite.flip_h:
	bullet.position = bullet.position + Vector2(-horizontal_offset, vertical_offset)
else:
	bullet.position = bullet.position + Vector2(horizontal_offset, vertical_offset)
1 Like

Hey i could not understand what you meant, im a little dumb :sweat_smile:

changed the bullet.position = position because when i did:
bullet.position = $Marker2D.position the Bullet wouldn’t
follow the character…

so did you mean like this?

func shoot():
	var bullet = Bulleto.instantiate()
	var horizontal_offset = -20
	var vertical_offset = -10

	bullet.position = position
	if sprite.flip_h:
		bullet.position = bullet.position + Vector2(-horizontal_offset, vertical_offset)
	else:
		bullet.position = bullet.position + Vector2(horizontal_offset, vertical_offset)
		
	get_parent().add_child(bullet)

Thank you for reaching out

So you were setting the bullet’s position twice. One was redundant and can make the code harder to debug.

First: bullet.position = position
Second: in the if/else block

For the glove problem: I don’t think it caused the issue you are seeing. I would guess you need to use global_position rather than position because you add_child() to another node. Global_position is the location in the scene, position is the offset from the parent node.

Try something like this:

func shoot():
	var bullet = Bulleto.instantiate()
	var horizontal_offset = -20
	var vertical_offset = -10

    bullet.global_position = global_position
	if sprite.flip_h:
		bullet.global_position = bullet.global_position + Vector2(-horizontal_offset, vertical_offset)
	else:
		bullet.global_position = bullet.global_position + Vector2(horizontal_offset, vertical_offset)
		
	get_parent().add_child(bullet)

This decouples the bullet’s position from any reference to the character.

Like this?

Try it. See if it works or not.

also try this , maybe that can fix it :sweat_smile: :joy:

bullet.position.x = $marker2D.position.x
bullet.position.y = $marker2D.position.y + 30
#for example 30

this code move it down :sweat_smile:

1 Like

didn’t :frowning:

thanks for the help tho.

maybe , in scene of bullet , the bullet is not in position (0,0)

check this , may that can to solve the problem