How do i stop my ball from losing momentum when it hits other objects?
Hello everyone, i am a super newbie at coding and i started making a block breaker game and ive gotten pretty far with it but ive run into a problem that is frustrating the heck out of me. every so often the ball will hit a brick or the paddle and slow down to a crawl. The ball has a circle collider2d with a "bounce" physics material attached to it- friction is 0, and bounce is 1. ive tried attaching the bounce material to the bricks and the paddle but still no luck, can you please help me fix this. this is what my ball script looks like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] float randomFactor = 0.2f;
public Transform paddle;
public GameManager gm;
public AudioClip bounceAudio;
public AudioClip paddleBounceAudio;
public AudioClip unbreakableAudio;
public AudioClip loseAudio;
// BITE SOUND ARRAY
public AudioSource bitesAudio;
public AudioClip[] audioClipArray;
Block block;
public float ballDeathTime = 0.5f;
Vector2 paddleToBallVector;
bool hasStarted = false;
Rigidbody2D myRigidBody2D;
// Start is called before the first frame update
void Start()
{
paddleToBallVector = transform.position - paddle1.transform.position;
myRigidBody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (gm.gameOver)
{
return;
}
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnMouseClick();
transform.position = paddle.position;
}
}
public void DestroyBall()
{
Destroy(gameObject, ballDeathTime);
}
public void BallPause()
{
myRigidBody2D.gravityScale = 0;
}
public void BallUnPause()
{
myRigidBody2D.gravityScale = 1;
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
myRigidBody2D.velocity = new Vector2(xPush, yPush);
}
}
private void LockBallToPaddle()
{
Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddlePos + paddleToBallVector;
}
private void OnCollisionEnter2D(Collision2D other)
{
Vector2 velocityTweak = new Vector2(Random.Range(0f, randomFactor), Random.Range(0f, randomFactor));
if (hasStarted)
{
myRigidBody2D.velocity += velocityTweak;
}
if (other.transform.CompareTag("BARRIER"))
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(bounceAudio);
}
if (other.transform.CompareTag("PADDLE"))
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(paddleBounceAudio);
}
if (other.transform.CompareTag("Unbreakable"))
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(unbreakableAudio);
}
if (other.transform.CompareTag("Breakable"))
{
bitesAudio = GetComponent<AudioSource>();
bitesAudio.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
bitesAudio.PlayOneShot(bitesAudio.clip);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag ("LOSE COLLIDER"))
{
gm.UpdateLives(-1);
hasStarted = false;
myRigidBody2D.velocity = Vector2.zero;
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(loseAudio);
}
}
}
Your answer
Follow this Question
Related Questions
How to fix minor jitter/stutter when moving a 2D character using physics in FixedUpdate() 0 Answers
How do I jump and how do I limit addforce 1 Answer
Can you use OnCollisionExit with an OverlapSphere? 1 Answer
My bullets don't fire properly 0 Answers
Why is new Vector3 stronger when player is in the air? 0 Answers