Why is my player character moving upwards?

Godot Version

Godot 4.6

Question

So I was programming a shooting mechanic for my player, and all I needed (at least for now) to do was to make it so the actual bullet itself moves upwards quickly when spawned in.

Here’s the bullet code:

extends CharacterBody2D

const bulletSpeed = 300.0

func _process(delta: float) -> void:
	position.y -= bulletSpeed * delta

For some reason, whenever the code ran it also shot the player upwards along with the bullet. I’m assuming since both the player and the bullet are both CharacterBody2D nodes that it kind of mushes the actions together, but I’m very new to Godot so I don’t entirely know myself.

Hard to say based on just this.

Did you assign the same script to your player node and the bullet?

I did not.

Is there anything in particular that you think would help you?

A screenshot of your node tree might help.

The script seems fine. Based on what I see there, any node that the script is assigned to should have its position.y modified by bulletSpeed * delta.

Right now, my only hypotheses are either:

  • You assigned this script to the player node by mistake.

  • The player node is a child of the bullet.

    Edit: Share the script of the player node as well, please.

Player Code:

extends CharacterBody2D

const SPEED = 300.0

@export var playerSprite : Sprite2D
@export var viewport : SubViewport

var bullet := preload("res://Scenes/bullet.tscn")

@onready var main = get_tree().get_root().get_node("Game")
@onready var timer: Timer = $Timer
	
func _physics_process(delta: float) -> void:
	var direction := Input.get_axis("leftMove", "rightMove")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	 
	move_and_slide()

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("shoot"):
		#print("Shooting!")
		
		var instance : Node2D = bullet.instantiate()
		instance.position.x = position.x
		instance.position.y = position.y
		
		main.add_child.call_deferred(instance)

Can’t send photos of my node trees right now, but it doesn’t seem like the player is a child node to my bullet node (unless the code above ends up making it that).

It could be that the bullet is colliding with the player node when spawning, therefore moving it too (pushing it). Try spawning the bullet some distance away to see what happens.

Edit:

	instance.position.x = position.x
	instance.position.y = position.y

I believe this causes the bullet to spawn inside the player node.

You are spawning the bullet directly on the player, and the bullet being a CharacterBody2D has some collision, so the player may snap out of and on top of the bullet. You probably do not need the bullet to be a CharacterBody2D, you don’t want enemies to walk into a bullet and stop moving as if hitting a wall. You could change the bullet’s type to an Area2D which can detect other bodies through the body_entered signal and apply damage, without stopping them.