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)
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.
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!
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.
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.