# 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

• 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.

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);
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).