Godot Version
v4.6.3 stable
Question
Being creating Pong this afternoon and now I’m trying to make the ball reset after clicking on a button on UI, but most of the time the ball maintains its position and changes it’s direction.
Maybe because I’m pausing with get_tree().paused, while if the ball hits one of the goals, it resets normally without pauses.
Heres the script if it helps any further:
extends RigidBody2D
class_name Ball
@onready var wall_hit_audio_stream: AudioStreamPlayer2D = $"../AudioManager/Wall Hit Audio Stream"
const SPEED = 100
var direction : Vector2
var time_last_goal : float
func ResetPosition():
time_last_goal = 0
position = Vector2.ZERO
rotation = 0
global_position = Vector2.ZERO
direction = GetRandomDirection()
func GetRandomDirection() -> Vector2:
var randomDirection := Vector2.ZERO
randomDirection.x = randi_range(0,1)
if randomDirection.x == 0: randomDirection.x = -1
randomDirection.y = randf_range(-1,1)
return randomDirection
func _process(delta: float) -> void:
time_last_goal += delta
func _physics_process(delta: float) -> void:
var true_speed = SPEED + time_last_goal
var collision = move_and_collide(direction * true_speed * delta)
if collision:
wall_hit_audio_stream.play()
var collider = collision.get_collider()
if collider is StaticBody2D: # Hit walls
direction.y *= -1
elif collider is CharacterBody2D: # Hit paddle
direction.x *= -1