- Home /
Question by
plaguebreath · Oct 04, 2019 at 02:28 PM ·
projectiletrajectoryparabolic
Question on projectile motion without using transform.translate or rigidbody on 3D with offset height between targets
Hello everyone, I'm try to write simple code for simulate the trajectory of a missile projectile that I try to shoot at certain angle, with a variabile gravity (for cheat the speed) and I know the start point and the target fix point with possibly offset height. Now I did ask help here percause I'm not really good in math so I'd like to understand what's the part I'm missing in my code for considering the offset of the target. Untill now I wrote this and if the target and the projectile start on same height it's work well:
float target_Distance = Vector3.Distance(transform.position, Target.position);
Vector3 toPosition = (Target.position - transform.position).normalized;
float angleToPosition = Vector3.SignedAngle(transform.forward, toPosition, transform.position);
float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
float Vx2 = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad) * Mathf.Sin(angleToPosition * Mathf.Deg2Rad);
float Vz2 = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad) * Mathf.Cos(angleToPosition * Mathf.Deg2Rad);
float flightDuration = target_Distance / Vx;
float elapse_time = 0;
Vector3 oldposition;
Vector3 newPosition = transform.position;
while (elapse_time < flightDuration || noInpact)
{
oldposition = transform.position;
float d = Time.deltaTime;
float newY = (Vy - (gravity * elapse_time));
newPosition += (new Vector3(Vx2 * d, newY * d , Vz2 * d));
transform.position = newPosition;
elapse_time += d;
}
now I need to consider the Y offset in here, but I'm not able to understand where, hope someone can help.
Comment
Could you edit your ask to display both blocks of codes as code?