Godot Version
3
I want that ball to move at random angles but not to be fixed angle like this, i want it to face to up.
extends KinematicBody2D
var speed = 750
var increase_amount = 10
var velocity = Vector2()
var collided = false
signal score_increment
func start(pos):
position = pos
var angle = rand_range(-PI / 6, PI / 6)
if randf() > 0.5:
angle = rand_range(5 * PI / 6, 7 * PI / 6)
rotation = angle
velocity = Vector2(speed, 0).rotated(rotation)
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
var collision = move_and_collide(velocity * delta)
if collision:
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
velocity = Vector2(speed, 0).rotated(rotation)
collided = true
print(speed)
else:
collided = false
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)
But its not facing down every time i just want to face only up.
Well, the start function seems to randomize, does it? Is the start random or not?
When else do you want it to randomize? After a collision?
yes its starts random, and yes i would like to change angle after collision to make it face not down every time
This might have been my fault you probably have to change this line:
velocity = Vector2(speed, 0).rotated(rotation)
# to this
velocity = velocity.normalized() * speed
1 Like
Can you print out the velocity before that?
1 Like
like this ?
func start(pos):
position = pos
var angle = rand_range(-PI / 6, PI / 6)
if randf() > 0.5:
angle = rand_range(5 * PI / 6, 7 * PI / 6)
rotation = angle
print(velocity)
velocity = velocity.normalized() * speed
collided = false
no in the physics_process-method after:
if collision:
velocity = velocity.bounce(collision.normal)
print(velocity)
just for testing purposes
1 Like
— Debugging process started —
Godot Engine v3.5.3.stable.official.6c814135b - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GTX 550 Ti/PCIe/SSE2
Async. shader compilation: OFF
Velocity not initialized
Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
— Debugging process stopped —
i think that has to be “==” and not “!=”:
if velocity != Vector2():
1 Like
okay
— Debugging process started —
Godot Engine v3.5.3.stable.official.6c814135b - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GTX 550 Ti/PCIe/SSE2
Async. shader compilation: OFF
Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
— Debugging process stopped —
yes but how i dont understand cuz its not moving
its on the center but colliding
Put a print here:
var collision = move_and_collide(velocity * delta)
print(collision)
collision has to be a value != null. I have to go now but maybe this helps
1 Like
okay no problem, thanks(yes its = null)
[Object:null]
Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
[Object:null]
[Object:null]
[Object:null]
ah i see now thats body entered isnt my ball thats my deatharea
extends Area2D
func _ready():
connect("body_entered", self, "_on_Area2D_body_entered")
func _on_Area2D_body_entered(body):
print("Body entered:", body.name)
if body.is_in_group("Ball"):
print("coll")
body.queue_free()
Just add the above lines from your start function to your physics process right before you set the velocity
1 Like
what is this
now its not rotating and its even more ugly thats before
extends KinematicBody2D
var speed = 750
var increase_amount = 10
var velocity = Vector2()
var collided = false
signal score_increment
func start(pos):
position = pos
velocity = Vector2(speed, 0).rotated(rotation)
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
var collision = move_and_collide(velocity * delta)
if collision:
var angle = rand_range(-PI / 6, PI / 6)
if randf() > 0.5:
angle = rand_range(5 * PI / 6, 7 * PI / 6)
rotation = angle
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
velocity = Vector2(speed, 0).rotated(rotation)
collided = true
print(speed)
else:
collided = false
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)
Cant i move at random direction not random angle and fixing my mouse angle ?
well, I will need to take a deeper look, but cannot right now. If you are still having issues I will look at it on several hours when I can focus on it.