- Home /

# Angle to Direction

I am making a tennis game. I am able to shoot the ball at the given target.. but the ball is moving in a straight line .. I want the ball to move at certain angles.. 30, 45, 60 so that I can make diffrent shots in my game ... loop Shot, Power Shot etc etc

"Right now the ball is going in the direction of the target and I've also figured out the angles for the height of the shot but the ball doesn't land exactly on the target."

*CODE UPTILL NOW:*

```
Vector3 direction;
direction = (target.transform.localPosition - ball.transform.localPosition).normalized;
direction.y = Mathf.Sin(30 * Mathf.PI / 180);
ball.rigidbody.AddForce(direction * 100);
```

I don't know what you are asking for here. What do the angles represent with respect to this drawing and the target? What is a 'power shot'? What is a 'loop shot'? How are you moving the ball...Rigidbody and AddForce or some other way? Posting your current code might get you a more accurate answer.

$$anonymous$$ake the Q more specific. Does 30 degrees mean thirty from a line straight down the court (global) or 30 from the way he is ai$$anonymous$$g (local)? Is the angle left/right (y) or up/down(x)? Or within a cone of thirty (to represent imperfect shots?)

**Answer** by robertbu
·
Oct 18, 2013 at 08:19 AM

Doing what you ask for here is a pretty complicated calculation. There are several detailed answers on UA on projectile motion and targeting. Do a bit of searching and they will turn up along with (if I remember correctly) dense functions that implement the functionality. But if you make a couple of simplifications and look at this problem a bit differently, you can solve it in a few lines of code.

For projectile motion, typically the horizontal velocity is split from the vertical velocity. Instead of calculating an angle, I'm suggesting you define your different shots in terms of horizontal velocity. Horizontal velocity will define how long the ball will take to be over the target.

Given a horizontal velocity, you can calculate time it will take for the ball to be over the target. So the question is how much vertical velocity do you give the ball so that it hits at the same time the horizontal velocity places the ball over the target. If you assume/place the target at the same height as the balls initial height, then the velocity needed is easy to calculate:

```
-gravity * time / 2.0;
```

So here is a bit of source code to demonstrate. Before integrating the concept into your code, I encourage you to play with it in a clean scene:

Start a new scene

Create a plane at (0.0, -0.5, 0.0), and scaled up larger (20,20,20).

Create a sphere and place it a the origin

Add a rigidbody to the sphere

Adjust the camera so that more can be seen: up on the 'Y', forward, and increase the angle of view.

Put a game object somewhere on the plane to act as a target

Add the following script to the ball

Drag the target game object onto the 'target' variable in the script.

Run the app and hit [Space] to launch the ball

# pragma strict

var target : Transform; var speed = 5.0;

function Update () {

`if (Input.GetKeyDown(KeyCode.Space)) { transform.position = Vector3.zero; // Start with the ball at the origin var toTarget = target.position - transform.position; // Ground vector to target toTarget.y = 0.0; var shotTime = toTarget.magnitude / speed; // Time to reach target var velocity = toTarget.normalized * speed; // Setup horizontal vector velocity.y = -Physics.gravity.y * shotTime / 2.0; // Upward velocity rigidbody.velocity = velocity; } }`

While the script is running, you can adjust the speed and the target position and hit [Space] again to relaunch the ball.

Here is the link to trajectory code if you want to solve this by angle:

http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Transform angle to grid position 2 Answers

how to find direction between two points 2 Answers

Why does gravity's vector3 act so weird? 1 Answer

Vector3.Angle() Never Reaching 0 1 Answer

Get Direction from 2 Vectors - and Apply to Transform 0 Answers