• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mrskeemz26 · Nov 16, 2019 at 08:05 AM · velocity2d collision

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);
         }
     }
 
    
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Crossfading happens too fast 2 Answers

How do you change the velocity of an object moving in a sine wave? 0 Answers

Best way to make a in game graph over time 0 Answers

Storing instantaneous velocity 0 Answers

How to remove all velocity for all prefab/clone objects in the scene 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges