x


Beginner bouncy balls

Hi

I am a beginner to unity. I have made a very basic game where I have 2 balls that bounce. Basically I now want to start with the balls on the ground and on a click of a button (developing for mobile devices) the balls will be pushed to make them bounce.

Any help??

Thanks

more ▼

asked Jul 18 '12 at 02:24 PM

hanimalP gravatar image

hanimalP
5 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can use GUI.Button to make a button, and make the balls start bouncing when you press it.

See also:

more ▼

answered Jul 18 '12 at 03:00 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks, but how do I make the balls bounce on the click of the button?? I assume this is scripted but not sure where to start?

Thanks

Jul 18 '12 at 03:37 PM hanimalP
(comments are locked)
10|3000 characters needed characters left

On the GUI script you can have something like:

private var ball1 : GameObject;
private var ball2 : GameObject;
// Declare strength for the bounce, you may need to edit this!
var bounceStrength : float = 100;

Start(){
// Find the references to your balls. Remember to create tags for your balls
// and assign those tags appropriately "Ball1" and "Ball2"
ball1 = GameObject.FindWithTag("Ball1");
ball2 = GameObject.FindWithTag("Ball2");
}

OnGUI(){
    // Button 1 for bouncing ball 1
    // Note that you'll have to replace Rect() variables with your own
    if (GUI.Button(Rect(posX, posY, btnWidth, btnHeight), "Bounce 1"))
    {
        // Add upwards force to the ball
        // Alternatively you can use Vector3(xForce, yForce, zForce)
        ball1.rigidbody.AddForce(Vector3.up * bounceStrength);
    }
    // Place button for ball 2 150 pixels to the right from the start of button 1
    if (GUI.Button(Rect(posX + 150, posY, btnWidth, btnHeight), "Bounce 2"))
    {
        ball2.rigidbody.AddForce(Vector3.up * bounceStrength);
    }
}

I hope this helps you get those balls bouncing. Just remember to assign the tags to the ball objects, add rigidbody components to the balls and of course change the Rect() variables of the buttons so you'll have them where you want them.

SheepHugger out.

more ▼

answered Jul 25 '12 at 11:53 AM

SheepHugger gravatar image

SheepHugger
86

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x342
x293

asked: Jul 18 '12 at 02:24 PM

Seen: 411 times

Last Updated: Jul 25 '12 at 11:53 AM