• 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 /
avatar image
0
Question by Ekta-Mehta-D · Mar 12, 2013 at 06:13 AM · translateballbounce

Bouncing Ball Effect

I want to add bouncing ball effect to my sphere object. I have added two empty game object with box collider which acts as a boundry or wall. But i am not finding the way to change the direction such that it gives effect like bouncing when the ball hits to those boundry. So what do i need to write on collision detection . I have tried code from this http://answers.unity3d.com/questions/28037/question-pongball-to-change-direction.html

But it goes out of my plane.

Script which i have written to shoot my ball is here:

 function Update ()
 {
       var hit  : RaycastHit ;
       var ray : Ray  = Camera .main .ScreenPointToRay (Input .mousePosition );
       if(Physics.Raycast (ray, hit))
       {     
          var tempx = hit.transform.position.x;
          var tempy  = hit.transform.position.y;
          velocity = Vector3(tempx,tempy,0);
       }
       
       if(velocity != null){
         // set the max move amount
         var moveAmount : float = 5f * Time.deltaTime;
         // translate the move amount
         transform.Translate(velocity * moveAmount);
      }    
 }

Please help me to solve my problem. Thanks for your help and support in advance..

Comment
Add comment · Show 7
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
avatar image AlucardJay · Mar 12, 2013 at 06:36 AM 0
Share

This is totally not an answer, nor a relative comment, but have you tried actually just using physics? Have a gameobject with a collider and a rigidbody, with the physics material set to bouncy ?

  • http://docs.unity3d.com/Documentation/$$anonymous$$anual/Physics.html

  • http://docs.unity3d.com/Documentation/Components/class-Physic$$anonymous$$aterial.html

avatar image Ekta-Mehta-D · Mar 12, 2013 at 06:41 AM 0
Share

yes that is the another way but i am making bubble shooter kinda game so if my ball collide with another ball , then it will continue to bounce??

avatar image AlucardJay · Mar 12, 2013 at 06:47 AM 0
Share

Absolutely. You can use rigidbodies for your bubbles, then write code so they only pop when they get shot, or they can have health and pop if they collide too many times. You can use AddForce or AddForceAtPosition to give extra 'bounce' on collisions, or even different bounce when colliding with different objects. If you are writing a game where you want things to look and feel and behave like physics, then absolutely use physics. You will also be writing much shorter scripts =]

avatar image Ekta-Mehta-D · Mar 12, 2013 at 06:52 AM 0
Share

but i want my object to stop bounce when they collide with another bubble.. and only bounce when they collide with my side walls

avatar image AlucardJay · Mar 12, 2013 at 06:55 AM 0
Share

again, totally possible and easy to code. Just set rigidbody.velocity to zero :

 rigidbody.velocity = Vector3.zero;

I'm just on a call in a que right now, I can give more information after. For now, watch :

  • http://www.unity3dstudent.com/2010/07/beginner-b00-adding-mass-gravity/

  • http://www.unity3dstudent.com/2010/07/beginner-b08-basic-force-movement/

avatar image Ekta-Mehta-D · Mar 12, 2013 at 06:57 AM 0
Share

ohk sir thanks a lot. i am trying. if i face any difficulty then i will ask..

avatar image saddam751 · Nov 26, 2014 at 05:37 AM 0
Share

what is bubble physics in unity3d?

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by AlucardJay · Mar 12, 2013 at 07:43 AM

Here is an answer that is totally different to the question, using physics and rigidbodies.

  • I tested this by making a new scene, then created a sealed room with 6 box colliders.

  • then created a sphere. I renamed this Bubble

  • To the Bubble sphere object, I attached a rigidbody component, and disabled gravity.

  • Then I imported the Unity Physics Materials (Standard Assets. Assets > Import Package > Physic Materials), then added the bouncy material to the sphere collider of the Bubble object.

  • Then I attached the following script.

  • Then I duplicated the Bubble object 20 times and scattered them around the room.

here is the script :

 #pragma strict
 @script RequireComponent( Rigidbody )
 
 var myTransform : Transform;
 var myRigidbody : Rigidbody;
 
 var forceToAdd : float = 2.0;
 
 function Start() 
 {
     myTransform = transform;
     myRigidbody = rigidbody;
     
     myRigidbody.velocity = Random.insideUnitSphere * forceToAdd;
 }
 
 function Update() 
 {
     
 }
 
 function OnCollisionEnter( other : Collision ) 
 {
     if ( other.collider.gameObject.name == "Bubble" )
     {
         myRigidbody.velocity = Vector3.zero;
     }
     
     // uncomment this part to add additional bounce 
     /*
     else
     {
         var localHitPoint : Vector3 = other.contacts[0].point - myTransform.position;
         var newHitForce : Vector3 = ( myTransform.position - other.contacts[0].point ).normalized * forceToAdd;
         
         //Debug.Log( "World hit point = " + other.contacts[0].point + " : Local hit point = " + localHitPoint + " : new Hit Force = " + newHitForce );
         
         myRigidbody.AddForceAtPosition( newHitForce, localHitPoint, ForceMode.Impulse );
     }
     */
 }
Comment
Add comment · Show 1 · Share
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
avatar image Ekta-Mehta-D · Mar 12, 2013 at 09:34 AM 0
Share

Thanks a lot sir. $$anonymous$$y problem has been solved..

avatar image
1

Answer by ccwiegreff · Mar 13, 2013 at 08:59 PM

on the box collider, click on Material then click Bouncy

Comment
Add comment · Share
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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

how do i make pinball ? 1 Answer

How do I check when a rigidbody is colliding with the floor? 2 Answers

A node in a childnode? 1 Answer

Unrealistic bounce 2 Answers

How do I get a sphere to bounce of a cube at an angle 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges