How to fix this?

Godot Version

3

My problem is that my ball sticks to my cat paws


ball.gd

extends KinematicBody2D

var speed = 400
var velocity = Vector2.ZERO
var rng = RandomNumberGenerator.new()

func _ready():
	rng.randomize()
	velocity = Vector2(speed, 0).rotated(rng.randf_range(0, PI * 2))

func _physics_process(delta):
	var motion = velocity * delta
	var collision = move_and_collide(motion)
	
	if collision:
		print("Collision detected with: ", collision.collider.name)
		if collision.collider.is_in_group("Paws"):
			velocity = velocity.bounce(collision.normal)
		else:
			velocity = velocity.bounce(collision.normal)

paw.gd

extends KinematicBody2D

export var move_distance: float = 550  # Distance to move left
export var move_speed: float = 1.5  # Speed of smooth transition

var original_position: Vector2
var target_position: Vector2

func _ready():
	original_position = position
	target_position = original_position

func _process(delta):
	if Input.is_action_pressed("ui_left"):  # Check if 'A' key is pressed
		target_position = original_position + Vector2(-move_distance, 0)
	else:
		target_position = original_position

	# Smoothly interpolate between the current position and the target position
	position = position.linear_interpolate(target_position, move_speed * delta)


the ball probably should be a rigidbody shouldnt it? What are the settings for the “motion” and “moving platform” of the ball?

Maybe yes, i thought that there is no difference between rigidbody2d and kinematicbody2d, and i dont understand what you mean setting of moving platform?

on the right in the inspector. There are tabs for your characterbody2d “Collision”, “Motion”, “Moving Platform”

Sorry for late response i will try soon


thats is my settings

I have changed my ball to rigidbody. but i dont understand why it is soo big


i change it but still same

okay i made it but as you said yes problem was that it was kinemticbody
but now my ball not bouncing good.

extends RigidBody2D

var score = 0
var initial_velocity = Vector2(500, 500)
onready var score_label = get_node("/root/Game/Timer")

signal ball_collided

func _ready():
	if score_label:
		linear_velocity = initial_velocity
	else:
		print("Score label not found.")

func _on_YarnMouse_body_entered(body):
	if body.is_in_group("Paws"):
		score += 1
		linear_velocity.x = -linear_velocity.x
		update_score_label()  # Update score after collision
	elif body.is_in_group("wall"):
		linear_velocity.y = -linear_velocity.y
		update_score_label()  # Optionally update score if needed here

func update_score_label():
	if score_label:
		score_label.text = str(score)
	else:
		print("Score label is not set.")

i think i just made worse, cuz now its even not nouncing and my score not working so i think the sscript is wrong

i fixed this too.

extends RigidBody2D

# Variables for ball movement and speed
export (float) var speed = 500.0
var velocity = Vector2()

func _ready():
	# Set the initial velocity of the ball
	velocity = Vector2(speed, speed).rotated(randf() * 2 * PI)
	linear_velocity = velocity
	angular_velocity = 0

func _physics_process(delta):
	# Keep the ball moving at constant speed
	if linear_velocity.length() > 0:
		linear_velocity = linear_velocity.normalized() * speed

func _on_YarnMouse_body_entered(body):
	# Handle collision with other objects
	func _on_YarnMouse_body_entered(body):
	# Handle collision with other objects
	if body.is_in_group("Paws") or body.is_in_group("wall"):
		var normal = (body.position - position).normalized()
		linear_velocity = linear_velocity.bounce(normal)


but i think its seems working not good,what you think ?


i have used print and there is nothing in my output i think my func is not working idk why

i think you dont need the func _on_YarnMouse_body_entered() method and for the physics_process:

if linear_velocity.length() < speed * delta:
    linear_velocity = linear_velocity.normalized * speed * delta

Im not sure if it works since i havent done a lot with rigidbodies and im also not sure what exactly you want to accomplish

1 Like

i just want my ball to be like a ping pong game ball

Add a physicsmaterial to your rigidbody2d and set bounce to 1 or a little lower depending on how much bouncines you want

1 Like

Thanks i will try it

Thanks, that worked but how to make it little bit faster after bouncing each time?

something like this:

func _physics_process():
    if get_colliding_bodies().size() > 0:
        speed += increase_amount
2 Likes

Maybe even better:

var contacs: int = 0
func _physics_process():
    var new_contacts = get_contact_count()
    if new_contacts > contacts:
        speed *= increase_amount
    contacts = new_contacts
1 Like


i dont know why its not working but anyway i cant try now.I think i put contacts to the wrong place i will change it tomorrow.

oh yeah this method doesnt exist in godot 3.5:

var contacs: int = 0
func _physics_process():
    var new_contacts = get_colliding_bodies().size()
    if new_contacts > contacts:
        speed *= increase_amount
    contacts = new_contacts
1 Like

Thanks, i will try.

extends RigidBody2D

# Variables for ball movement and speed
export (float) var speed = 1000
var velocity = Vector2()
var increase_amount = 10
var contacts: int = 0

func _ready():
	# Set the initial velocity of the ball
	velocity = Vector2(speed, speed).rotated(randf() * 2 * PI)
	linear_velocity = velocity


func _physics_process(delta):
	var linear_velocity = velocity
	var new_contacts = get_colliding_bodies().size()
	if linear_velocity.length() < speed * delta:
		linear_velocity = linear_velocity.normalized * speed * delta
	if new_contacts > contacts:
		speed *= increase_amount
	contacts = new_contacts

Its slowing down very fast and i think my speed increasing dont work.