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*.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;
}
}
}