When I Try To Move The Player, THe Whole Game Scene Moves

Godot Versio4.3

Question

I am trying to modify a pong game into a tennis game> I have created two of the necessary game objects, the player and the ball. The problem is that when i try to move the player the whole game scene is moving in and out of the viewport, but the player and ball are not moving.

My code for the player and ball are as follows:

Player

extends CharacterBody2D
const SPEED := 200.0

func getYDir() -> float:
	return Input.get_action_strength("move_left") - Input.get_action_strength("move_right")

func _physics_process(delta: float) -> void:
	
	var dir :Vector2=Vector2(0, getYDir())
	velocity = dir * SPEED
	move_and_slide()

Ball

extends CharacterBody2D

const SPEED := 400.0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	velocity = Vector2(-SPEED, 0)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	var col := move_and_collide(velocity)
	if col:
		var normal := col.get_normal()
		velocity = velocity.bounce(normal)

Can anyone help?

Is the ball and camera a child of the player? Can you share a screen shot of your scene tree?

this my player scene tree:

image

this is my ball scene tree

image

the ball scene is instanced from the player scene

i might have missed something really stupid- the camera

Notice here your Background, and Ball is a child of the Player. When the Player moves, all of it’s children move with it. You will need another scene for the “Level” or “World”, with Player, Ball, and Background all as siblings.

2 Likes

I did what you said and made different scenes for ball and background, but how do i now properly assemble it. Sorry for the noob question!

instancing results in the same player child behavior!

The world/level scene should look something like this

World
↦ Background
↦ Player
↦ Ball

Paste as siblings is disabled in the scenes, should i try to recreate the project?

Why is the ball in the same scene as the player? (Also you fixed the issue that I pointed out in the deleted post)

I have modified that, now the ball, the background and the player all have their separate scenes. The problem I am having is that I am unable to create sibling scenes! and I dont know how to assemble all the separate scene components into a single game!

It is a sibling if it shares the same parent, nothing special. in this case everything should be a child of the “World”

World would be a simple node right?

Yup Node2D works, even Node.

I tried dividing the game components into separate
background
player
ball
collision
scenes.

everything is alright in the player background(world) 2d scene editor, but when i try to preview the game i can only see the moveable player with her rectangle collision.

When you have a sec, can you share a screenshot of your full tree again (everything under World)?

i got it working. i am now facing another issue:

The scene tree can be a bit confusing at first. So a couple things I would mention.

First: All children will inherit the parent transform, including position. And in this case movement.

Second: The scene root cannot have siblings, unless inside another scene.

Third: When chaging parent, the inherited transform is diferent. You may need to reset the position. Or scale. Can you see everything in the editor fine?

Thats so far what I would check at this point, with the current info.

1 Like