Hello guys!
What i am looking for is a formula to calculate vertical and horizontal (or just the initial) speed for projectile to hit a target which is at a height different than launcher.
I have been looking for the answer for quite a long time and actually found solution for the same height.
Here is the code sample for that:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PArticleProjectile : MonoBehaviour {
[Range(10,45)]
public int Angle = 45;
public float gravity = 9.8F;
public Transform Target;
public ParticleSystem.VelocityOverLifetimeModule PartS;
void Start ()
{
PartS = gameObject.GetComponent<ParticleSystem>().velocityOverLifetime;
}
void Update ()
{
Vector3 displacement = (transform.position - Target.position);
float Vx = Mathf.Sqrt((distance * gravity) / Mathf.Sin(2 * Angle * Mathf.Deg2Rad)) * Mathf.Cos(Angle * Mathf.Deg2Rad);
float Vy = Mathf.Sqrt((distance * gravity) / Mathf.Sin(2 * Angle * Mathf.Deg2Rad)) * Mathf.Sin(Angle * Mathf.Deg2Rad);
PartS.x = Vx;/*SolveBallisticEquation(displacement.x, displacement.y, gravity, Angle).x;*/
PartS.y = Vy;/*SolveBallisticEquation(displacement.x, displacement.y, gravity, Angle).y;*/
}
}
It works perfectly when target and launcher are on the same height but it starts to deviate when i am changing altitude of either one.
By the way i want to do this via particle system(Velocity over lifetime).When i enable gravity on particles velocity over lifetime starts to behave like initial velocity.
So if anyone has some useful formulas for that i’d appreciate if you share them down in the answers.