Godot Version
4.2.2
Question
having problems with my script and game. I’m trying to make pong.
my issues are the ball knocking the paddle back when the ball collides with it (im using character body 2d for the paddles) and whenever the ball make contact with the either of the collision shapes i set up on the right and left i receive an error.
whats supposed to happen is the ball should reset but i get a "Invaild set Index ‘global_postion’ " error instead. bellow i linked my script nodes and video that i am following along with. any help is appreciated
video:
here are my nodes
here is my game, player and ball script in that order
game : extends Node2D
func _on_top_body_entered(body):
print(“out”)
body.direction.y *= -1
func _on_bottom_body_entered(body):
print(“out”)
body.direction.y *= -1
func _on_left_body_entered(body):
body.queue_free()
var e = preload(“res://ball.tscn”).instantiate()
add_child(e)
e.global_postion = Vector2(576,320)
func _on_right_body_entered(body):
body.queue_free()
var e = preload(“res://ball.tscn”).instantiate()
add_child(e)
e.global_postion = Vector2(576,320)
player :
extends CharacterBody2D
const SPEED = 300.0
@export var side = ‘p1’
func _physics_process(delta):
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction
if side == 'p1':
direction = Input.get_axis("up", "down")
else:
direction = get_axis("Lup", "Ldown")
if direction:
velocity.y = direction * SPEED
else:
velocity.y = move_toward(velocity.y, 0, SPEED)
move_and_slide()
func get_axis (up, down):
if Input.is_key_pressed(KEY_I): return -1
elif Input.is_key_pressed(KEY_K): return 1
balll:
extends CharacterBody2D
const SPEED = 300.0
var direction = Vector2.ZERO
func _ready():
direction.y = [1-1].pick_random()
direction.x = [1,-1].pick_random()
func _physics_process(delta):
if direction:
velocity = direction * SPEED
else:
velocity = velocity.move_toward(Vector2.ZERO, SPEED)
move_and_slide()