It seems that in my game there is still a bug with the ball slowing down. in the short lecture about friction we had to set it to 0 so the ball doesn’t slow down when hit by the sides of the paddle.
Now I tested autoplay and after that manually and it seems that my ball slows down or sometimes even speeds up when (mostly) hitting unbreakable blocks. Sometimes the ball speeds up so much that it start going through the blocks. So I played around with the friction and velocity to see if it changes but it doesn’t. Does anyone have an idea or the same issue?
Hi Kederin,
Have you already tried to remove the Physics2D material from the ball and the paddle? And have you already tried to set the Collision Detection of the ball’s Rigidbody2D component to “Continuous”?
I unassigned and reassigned the material and put the collision detection on “continuous”
It seems to go a bit better, but the ball still slows down/ speeds up while playing (mostly after a minute of gameplay). I provided a screenshot from my paddle and ball RigidBody2D
Ball:
Paddle:
Maybe I’m overlooking something in there. Thanks in advance
Remove the Bounce material completely from the components. Leave the Collision Detection mode set to “Continuous”.
Did you already implement Rick’s tweak which is supposed to prevent the ball from getting into a “boring loop”?
Yes the tweak is implemented, I removed the material from both components. I will put my ball script down here.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
// configuration parameters
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] AudioClip ballSounds;
[SerializeField] float randomFactor = 0.2f;
// state
Vector2 paddleToBallVector;
bool hasStarted = false;
//cached componenent references
AudioSource myAudioSource;
Rigidbody2D myRigidBody2D;
// Start is called before the first frame update
void Start()
{
paddleToBallVector = transform.position - paddle1.transform.position;
myAudioSource = GetComponent<AudioSource>();
myRigidBody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnMouseClick();
}
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
myRigidBody2D.velocity = new Vector2(xPush, yPush);
hasStarted = true;
}
}
private void LockBallToPaddle()
{
Vector2 paddleposition = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddleposition + paddleToBallVector;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 velocityTweak = new Vector2
(UnityEngine.Random.Range(0f, randomFactor),
UnityEngine.Random.Range(0f, randomFactor));
if (hasStarted)
{
AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
myAudioSource.PlayOneShot(clip);
myRigidBody2D.velocity += velocityTweak;
}
}
}
In that case, the tweak
is causing the problem. Not your fault. Test a lower randomFactor
value, for instance 0.02f.
That might have done it, I’m going to make more levels, then I will test more. I will give some feedback on this matter after that. If the ball stays around the same speed I will mark your reply as the solution.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.