It should be the same despite where it’s colliding
I’ve kept on trying, but I can’t seem to find how to fix this bug. This is really tiring me
could you post the entire script again?
Here:
extends CharacterBody2D
@export var speed = 600
var score = 0
var can_add_score = true
var speed_increase = 10
@onready var label = $"../Score"
@onready var game_over_screen = $"../GameOverScreen"
@onready var hit_sound = $Hit_Sound
@onready var hit_sound_2 = $Hit_Sound2
@onready var pop_sound = $Pop_Sound
@onready var game_over_sound = $Game_Over_Sound
func _ready():
visible = true
var direction = 1
direction = randi_range(1, 4)
if direction == 1:
velocity = Vector2.RIGHT.rotated(PI/4)
elif direction == 2:
velocity = Vector2.LEFT.rotated(PI/4)
elif direction == 3:
velocity = Vector2.LEFT.rotated(PI/-5)
elif direction == 4:
velocity = Vector2.RIGHT.rotated(PI/-5)
func _physics_process(delta: float) -> void:
var collision = move_and_collide(velocity * speed * delta)
if not collision:
return
if collision.get_collider().name == "Paddle":
if collision.get_normal() != Vector2.UP:
# side of paddle
velocity = velocity.bounce(collision.get_normal()) * speed
if collision.get_collider().name == "Paddle" and %Timer.is_stopped():
score += 1
label.text = str(score)
%Timer.start()
speed += 10
print(speed)
hit_sound_2.play()
hit_sound.play()
if collision.get_collider().name == "Borders":
hit_sound.play()
if collision.get_collider().name == "DeathArea":
pop_sound.play()
game_over_sound.play()
visible = false
game_over_screen.visible = true
get_tree().paused = true
if collision.get_collider().name == "Paddle" or "Borders" and %Dust.emitting == true:
Spawn_Dust_2(collision.get_position())
else:
Spawn_Dust(collision.get_position())
velocity = velocity.bounce(collision.get_normal())
func _on_timer_timeout():
%Timer.stop()
func _on_play_again_pressed():
print("Restarted")
game_over_screen.visible = false
get_tree().paused = false
get_tree().change_scene_to_file("res://Scenes/Game.tscn")
func _on_menu_pressed():
print("Menu button clicked!")
game_over_screen.visible = false
get_tree().paused = false
get_tree().change_scene_to_file("res://Scenes/Start_Menu.tscn")
func Spawn_Dust(collision_point: Vector2) -> void:
%Dust.global_position = collision_point
%Dust.emitting = true
%Dust.restart()
func Spawn_Dust_2(collision_point: Vector2) -> void:
%Dust2.global_position = collision_point
%Dust2.emitting = true
%Dust2.restart()
Once again, I still feel like it has something to do with the collisions, but I don’t know still.
Maybe use a RigidBody2D and LockRotation in the inspector. It would help with scattering around. You don’t really need that for Pong.
Changing the node types feels pretty unnecessary at this point. Once again I think it has something to do with the code, Might have to send a video to demonstrate the problem
Well, let’s not rip the script like that. Just so. I think what’s happening from my Pong experience is the ball strikes the paddle. The paddle passes the collision to the floor. Right? You need to kill the player from the collision of the ball to the floor. Not with the collision of the paddle.
I’m not sure about using any floor in my collision. Check my script that’s above, It should help show what’s happening
It’s because you got some code from a dump. Do you really think I can’t tell if a code is from a naked science collision or a new trial? First of all. It’s your vectors. 90 degrees minus 90 degrees is 0. So it’s the same spot in the position. You’re standing still already.
The direction variable is supposed to set where the ball will move to, it has nothing to do with the collision. I’ve been trying to find where the problem is, but I’m not really seeing where it’s happening, I’m going to have to record a video on what the problem looks like
I don’t understand why I have to explain this. Do you understand the difference between front hand and back hand? Your ball will always rotate by 90 degree steps. Probably at delta. And, because of _ready(). And, because your collision always starts on the right side of the screen. So, imagine it’s turning clockwise all the time. Here you go! Try to stop it with the paddle. Pitting that rotation to 0. Because it’s upside down. Can I also express how difficult it will be to ask you to experiment. Try this exact same thing. But, have the ball start with striking the left side of the screen first. Just, don’t go asking what the difference might be.
Let’s have a fare wager? I bet you don’t understand. That because of this setting at delta and the velocity the ball never stops moving and turning after collisions.
Here’s that video:
I could try out what you just suggested, but I’m not really sure which area of my code your talking about that needs changing. By striking the left side of the screen, do you mean I should make the ball go from the left or are you talking about the (PI/4) that’s there? I can’t really tell without a proper explanation.
I’m sorry if I’m not understanding what you’re actually talking about, I’m still learning the engine and some of this stuff is still pretty confusing to me. Can you be little bit clearer on the steps I actually have to take to fix this ?
if you want. you can track it like this. It wouldn’t help to understand why it has to be like that. But, there must be opposites at all times anyway.
If you look at the video, the ball starts at a random point and moves in a different direction, this is what the script does.
But this isn’t really doing anything with my main problem, watch that video closely and you’ll see what I’m talking about. How do I fix it?
You still have this second bounce call, I believe removing this wil fix your issue
gionggone is really hard to understand, they might be translating. All of their posts are a tough read. If they seem antagonistic I wouldn’t take it personally.
But I’m using it to access the side of the paddle, what else would I use to it then?
You wouldn’t, you don’t have to. You are already accessing the side of the paddle by having a collision shape and using move_and_collide
. this is infact double-bouncing on the side of the paddle specifically, which is our issue.
The velocity = velocity.bounce(collision.get_normal())
at the end of the physics process handles bouncing from all sides of the paddle and all sides of the walls; every collision shape.
Alright then, I’ll remove it
It once again doesn’t really make a difference, What’s really going on here?