0 votes

Hi,
I am making a pong clone and the ball bounces back when it hits a paddle with area_entered. The bouncing back works finely, but i also want to make it so if the ball is hitting the left side of the paddle it bounces left, and if it is hitting the right side of the paddle it bounces right. This means i have to compare the position of the ball to the position of the paddle. I have tried several methods of trying to access the position property of the paddle, and have been searching the internet for answers for a long time, but i can't figure it out. So as stupid of a question as it may be, how do i access the position of the paddle? The paddle is a different scene from the ball and i am accessing the position property of the paddle.
Here is the code:

func _on_Ball_area_entered( area ):
if area.get_name() == 'Player Paddle':
    motion.y = yspeed
    if position.x > ?????:
        print("greater than!")
in Engine by (241 points)
edited by

1 Answer

+1 vote
Best answer

I'm also new to godot and ran into a similar problem with something I'm working on. What I did to solve this was to create a higher level scene that uses an instance of both the paddle and the ball. (right next to the + icon to create a new scene there is a chain icon to add an instance of another scene, or you can do through code.)

then you can access the properties of those instance with the $

var paddle_pos = $paddle.get_position()

an example of adding scenes in code:

    extends Node2D

var paddle = load("res://Scenes/player.tscn")
var balls = load("res://Scenes/ball.tscn")

func _ready():
    var player = paddle.instance()
    var ball = balls.instance()
    add_child( player )
    add_child( ball )

    #NOTE: no $ sign when using variable name
    var player_pos = player.get_position()

LIke I said, I'm new to this too, but that's something like what I did and worked for me.

Garrett

by (70 points)
selected by

Oh yeah! I already have them in instanced scenes in the main scene, but i forgot i could control both of them from a script in the main node. Thank you!

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.