Godot Version
3
My mouse not every time but sometimes at start starts moving like this and after it cant bounce normaly.
extends KinematicBody2D
var speed = 750
var increase_amount = 10
var rotation_speed = 2 # Adjust this value for rotation speed
var velocity = Vector2()
var collided = false
signal score_increment
func start(pos):
position = pos
# Ensure the ball starts moving at an angle not too close to horizontal
var min_angle = deg2rad(45)
var max_angle = deg2rad(270)
var initial_angle = rand_range(min_angle, max_angle)
velocity = Vector2(speed, 0).rotated(initial_angle)
# Check if velocity is too horizontal and reassign if necessary
while abs(velocity.angle()) < min_angle or abs(velocity.angle()) > max_angle:
initial_angle = rand_range(min_angle, max_angle)
velocity = Vector2(speed, 0).rotated(initial_angle)
collided = false
func _ready():
randomize()
# Ensure the ball starts moving when the game starts
start(position)
func _physics_process(delta):
if velocity != Vector2(): # Check if velocity is initialized
# Move the ball and check for collision
var collision = move_and_collide(velocity * delta)
if collision:
# Reflect the velocity based on the collision normal
velocity = velocity.bounce(collision.normal)
if collision.collider.is_in_group("Paws") and not collided:
print("Collided with Paw")
emit_signal("score_increment")
speed += increase_amount
# Maintain direction but increase speed
velocity = velocity.normalized() * speed
collided = true
print(speed)
else:
collided = false
# Apply rotation
rotation += rotation_speed * delta
else:
print("Velocity not initialized")
# Call this function to start the ball movement with initial position
func init_ball(start_pos: Vector2):
start(start_pos)