Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to make ball randomly bounce around in box?

Discussion in 'Scripting' started by pizzagirl4eva, May 18, 2018.

  1. pizzagirl4eva

    pizzagirl4eva

    Joined:
    Oct 18, 2017
    Posts:
    6
    Hi, I have an English project in which I'm making a game for, and in the game I need to have an eyeball randomly bounce around in a box (side to side, up and down, completely at random). I tried placing in a ball with a very bouncy physics material, yet it only bounced up and down. Any help?

    Thanks, Olivia.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,113
    How random do you want it to bounce? Unity's physics engine will try to be realistic. If you want it to bounce around, you'll need to give it an initial push in some direction. You can use AddForce() on the rigidbody to get it moving in a direction, and let the physics engine take over.

    Note that too bounce for a while, you'll need to add a Physic Material with a high bounce value to the ball.

    If you really want unrealistic, random bounding, one approach is to make the ball spin. When it lands while spinning, the spin will cause it to bounce in a different direction. This script can be added to the object to cause it to bound randomly.

    Code (CSharp):
    1.     public class RandomBounce : MonoBehaviour
    2.     {
    3.         Rigidbody _rb;
    4.         void Start()
    5.         {
    6.             _rb = GetComponent<Rigidbody>();
    7.  
    8.             // Increase max angular velocity or we won't see much spin.
    9.             _rb.maxAngularVelocity = 1000;
    10.  
    11.             StartCoroutine(ChangeRotation());
    12.         }
    13.  
    14.         private IEnumerator ChangeRotation()
    15.         {
    16.             while (true)
    17.             {
    18.                 _rb.AddTorque(new Vector3(10 * UnityEngine.Random.Range(0, 1f), UnityEngine.Random.Range(0, 1f), UnityEngine.Random.Range(0, 1f)), ForceMode.Impulse);
    19.                 yield return new WaitForSeconds(1);
    20.             }
    21.         }
    22.     }
     
  3. pizzagirl4eva

    pizzagirl4eva

    Joined:
    Oct 18, 2017
    Posts:
    6
    Hey! Thanks so much for the fast reply! I put the code into the project and messed with it a little and got some good results! Thanks, hopefully I'll get a good grade!

    Code (CSharp):
    1. public class BallBounce : MonoBehaviour
    2. {
    3.   Rigidbody _rb;
    4.         void Start()
    5.         {
    6.             _rb = GetComponent<Rigidbody>();
    7.             // Increase max angular velocity or we won't see much spin.
    8.             _rb.maxAngularVelocity = 1000;
    9.             StartCoroutine(ChangeRotation());
    10.         }
    11.         private IEnumerator ChangeRotation()
    12.         {
    13.             while (true)
    14.             {
    15.                 _rb.AddTorque(new Vector3(10 * UnityEngine.Random.Range(0, 3f), UnityEngine.Random.Range(0, 3f), UnityEngine.Random.Range(0, 3f)), ForceMode.VelocityChange);
    16.                 yield return new WaitForSeconds(1);
    17.             }
    18.         }
    19. }