- Home /

# Projectile trajectory calculation for 3 dimensions

Hey, I have been able to calculate my trajectory parabola for just axis **y** and **z**. I've been lost recently by trying to comprehend what should it look like in the 3rd dimension. Can't figure it out. I have used the known formula from Newton in order to find height of a point according to its distance. But this formula does't cover the 3rd dimension, unfortunately. Basically I need my trajectory to be drawn before and till the hit point of my raycast starting from my cam position. Here is my code, help is appreciated.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Trajectory : MonoBehaviour {

```
public GameObject point;
public float angle = 30.0f;
public int pointsNum = 30;
public float Vinit = 20.0f;
public List<GameObject> Points = new List<GameObject>();
Vector3 targetPoint = new Vector3(0, 0, 0);
float distance = 0;
void Start()
{
CreatePoints(pointsNum);
}
void Update()
{
PositionPoints();
Debug.Log(CheckAngle() + " ; " + CheckDistance());
}
void CreatePoints(int num)
{
for (int i = 0; i < num; i++)
{
GameObject pointPrefab = Instantiate(point, transform.position, Quaternion.identity) as GameObject;
pointPrefab.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
Points.Add(pointPrefab);
}
}
void PositionPoints()
{
for (int i = 0; i < Points.Count; i++)
{
Points[i].transform.position = new Vector3(0,
1 + CalcHeight(CheckAngle(), (i + (CheckDistance() / Points.Count)), Vinit),
i + (CheckDistance() / Points.Count));
}
}
float CalcHeight(float angle,
float distance,
float Vinit)
{
float Vvert = Vinit * (Mathf.Sin(angle * Mathf.Deg2Rad));
float Vhor = Vinit * (Mathf.Cos(angle * Mathf.Deg2Rad));
float x = distance;
float t = distance / Vhor;
float g = Physics.gravity.y;
float u = Vvert;
float s = (( u * x ) / Vhor ) + 0.5f * g * Mathf.Pow( t, 2 );
return s;
}
float CheckAngle()
{
Vector3 groundVector = Vector3.forward;
Vector3 cameraVector = Camera.main.transform.forward;
float angle = Vector3.Angle(groundVector, cameraVector);
return angle;
}
float CheckDistance()
{
float rayDist = 700.0f;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, rayDist))
{
Debug.DrawRay(ray.origin, Camera.main.transform.forward * rayDist, Color.red);
targetPoint = hit.point;
distance = hit.distance;
return distance;
}
else
{
return distance;
}
}
```

}

**Answer** by Firedan1176
·
Dec 18, 2014 at 09:48 PM

What you could try is to get the velocity of the object you're shooting at and add that to the "target position" of your mechanism. So even if it's traveling away from the camera, it will still shoot at the correct location, but this implies that it takes exactly 1 second to reach the target no matter the distance.

I'm surprised it wasn't easy to find the answer, but a simple formula to calculate trajectory with the projectile's speed and the distance from the target:

```
float angle = (Mathf.Asin ((Physics.gravity.magnitude * distance) / (velocity * velocity)) / 2) * 100;
```

And you can incorporate this into a simple formula to find a target position.

```
public float bulletSpeed;
//This is purely for effect, just makes it rotate smooth
public float rotationSpeed;
//This is a custom script that calculates the velocity of an object.
//Because MY "test enemy" didn't have a rigidbody, I used code to calculate vel.
Vector3 enemyVelocity = enemy.GetComponent<Velocity>().velocity;
//Get the distance to the target as a float
float distance = Vector3.Distance (transform.position, enemy.transform.position);
//Get the number in seconds to reach the target.
float timeToReachTarget = (distance / bulletSpeed);
//This will be what we rotate towards (the "invisible target")
Vector3 targetPosition = (enemy.transform.position - transform.position);
//We will add the "tracking" stuff to make it aim up and ahead
targetPosition += (enemyVelocity * timeToReachTarget) + (Mathf.Asin((Physics.gravity.magnitude * dist) / (vel * vel)) / 2) * 100);
//This is purely for effect, just makes it rotate smooth
Quaternion tempRotation = Quaternion.LookRotation(pos1);
transform.rotation = Quaternion.Lerp(transform.rotation, tempRotation, rotationSpeed);
```

Problems: If your enemy doesn't have a rigidbody, you need to use code to calculate. If your enemy has a rigidbody, you need to get it and change the "Vector3 enemyVelocity" to get that rigidbody.velocity(???). If your enemy moves randomly and not at a constant speed, it won't work well.

I hope this is what your wanting.

This is script I have made before and if it doesn't work for you, I will try to fix it. I can also include a link to the script I made if you want to look at that.

### 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

Physics.Raycasy not really using Layermask? 1 Answer

SphereCast Arguments 1 Answer

SphereCastAll doesn't seem to be working 2 Answers

How to find what type of tile player is on in 2D game 1 Answer