Hey,

I want to shoot a spell to a **specific location (= target)**, but the spell has a max range.

The **target position** in the picture is what I want to determine.

I know:

camera position

spawn position

distance between camera and spawn position

extended camera position ( done with Camera.ScreenPointToRay() )

angle between camera and spawn location

I tried the law of cosines but I am missing the angle between target and spawn position.

Currently, I am using:

float distanceToTarget = maxRange + Vector3.Distance(spawnLocation, mainCamera.transform.position);

Vector3 target= mainCamera.transform.position + (crosshairRay.direction * distanceToTarget );

But the distance from spawn location to target is about 11, depending on the view angle of the camera.

It should be 10.

Any ideas? Thank you very much.

I finally found the solution by using the law of sinus

**b / sin(beta) = c / sin(gamma)**

```
b = maxRange = 10
c = Vector3.distance(mainCamera.transform.position, spawnLocation)
beta = Vector3.Angle(mainCamera.transform.forward, spawnLocation - mainCamera.transform.position);
```

Now I can calculate gamma

```
gamma = Mathf.Asin((c / maxRange) * Mathf.Sin(beta * Mathf.Deg2Rad));
```

*Note: you need radians in sinus*

As I know 2 angles in this triangle, I can calculate alpha

```
alpha = 180 - (gamma * Mathf.Rad2Deg) - beta;
```

Using law of sinus again, I can calculate a

```
a = b/ Mathf.Sin(beta * Mathf.Deg2Rad) * Mathf.Sin(alpha * Mathf.Deg2Rad);
```

Finally my target position is

```
Vector3 target= mainCamera.transform.position + (mainCamera.transform.forward * a);
```

I hate math…

Is “target” an object you are going to hit? If yes then I suggest using Raycast to get the position and avoid all the trouble.

If you really want to calculate the position of the virtual target, you probably need to calculate the distance between the point and ray (distance between a ray and a point - Questions & Answers - Unity Discussions) to simplfy it into a right angle triangle problem.

I am not good at math

For SAS he needs to know the length of the camera-extended (the target position), which he doesn’t.

He has SSA: the angle between camera-extended and the side to the spawn-point, distance between camera and spawnpoint, distance from spawnpoint to target (10)