x


how to launch a ball?

Hi, I'm new here and also new to unity. I have done some web development and know a little javascript so I think that will be my language of choice.

Basically I want to create a game where when you press the button, a ball will travel on a trajectory and then hit the ground and roll to a stop.

The keys to this is that the ball slows down when it hits the ground and doesn't roll too far.

So far I have created a sphere on my terrain. I have been searching for how to launch projectiles and moving objects to a position but I have not found any useful code.

If someone could point me in the right direction or offer a sample of what my script will need to look like to achieve this I would be very greatful.

Thanks

Steve

more ▼

asked Jun 18 '12 at 11:58 AM

binga30 gravatar image

binga30
2 1 1 1

If I understood you right the main point is the slowing down of the ball?

Just attach a rigidbody to the ball and the rest should work... For instantiating an Object look here:

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

Jun 18 '12 at 12:03 PM bubblegumsoldier

I think I might have replied in the answer box lol.

Anyway yeh the slowing down is important, as if the effects of the terrain when the ball lands, ie- the ball would roll down a slope.

However my first step is to get the ball launching into the air on a trajectory when you press click a button or press a key.

Thanks for your link, I am not familiar with the instantiate methods. Is this how you make projectiles fly through the air to a designated position?

Thanks again

Jun 18 '12 at 12:17 PM binga30
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
  • Attach a rigidbody and a sphere collider to your ball.
  • Attach a collider to the ground.
  • choose a physics material for your ground and your Ball's colliders. This affects bounciness, friction etc. there is a standard asset package with some basic materials.
  • when you get your key press, use rigidbody.AddForce to propel your ball into the air.
more ▼

answered Jun 18 '12 at 12:21 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 100

Mike thanks for your answer. I must admit I am a little more newb than I would like. Using your solution, how would I specify the trajectory for the ball. It would reach its highest point at about 80% of its distance. Would i be asking too much to get a sample of what the script would look like? Thanks again

Jun 19 '12 at 07:34 AM binga30

Although rather brief on details this answer got me what i wanted.

Jul 09 '12 at 10:08 AM binga30
(comments are locked)
10|3000 characters needed characters left

You're gonna need to use some Physics for this (hopefully I'm not a Unity moron, and Unity actually has realistic Physics calculations). Assuming we have that set relation of 80%, here's what it looks like (in C#, because I don't write in Javascript on Unity since it's different from the ECMA standard):

float y = 50;
float dX = y * 0.8f;

// y = (vNotY ^ 2) / 2g
float vNotY = y * 2 * Physics.gravity.y;
vNotY = Mathf.Sqrt(vNotY);

// t = 2 * v0y / g
float time = 2 * vNotY / Physics.gravity.y;

// v = d / t
float vX = dX / time;

Vector3 force = new Vector3(vX, vNotY, 0);
ball.rigidbody.AddForce(force);

I'm HOPING this works. I haven't tested this and I haven't worked with the Physics part of Unity (so I can't guarantee what force in AddForce() actually represents. I also haven't taken Physics in a few years (this actually took forever because I suck at trajectories now T_T).

more ▼

answered Jun 19 '12 at 08:08 AM

Mizuho gravatar image

Mizuho
392 3 5 6

(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:

x3460
x1877
x1281
x51

asked: Jun 18 '12 at 11:58 AM

Seen: 579 times

Last Updated: Jul 09 '12 at 10:08 AM