- Home /
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
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
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
Answer by whydoidoit · Jun 18, 2012 at 12:21 PM
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.
$$anonymous$$ike 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
Although rather brief on details this answer got me what i wanted.
Answer by Mizuho · Jun 19, 2012 at 08:08 AM
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).