[UNITY][C#] Projectile by tosawat
Jan0
IEnumerator projectile(Transform trans, Vector3 from, Vector3 to, float height, float time)
{
trans.localPosition = from;
yield return null;
float Vx = (to.x – from.x) / time;
float Vy = 2 * height / (0.33f * time);
float A1 = -2f * height / Mathf.Pow(0.33f * time, 2);
float t = 0;
while (t <= 0.33f * time)
{
float deltaTime = Time.deltaTime;
float deltaSx = Vx * deltaTime;
Vy += A1 * deltaTime;
float deltaSy = Vy * deltaTime;
trans.localPosition = new Vector3(trans.localPosition.x + deltaSx, trans.localPosition.y + deltaSy);
t += deltaTime;
yield return null;
}
Vy = 0;
float A2 = -2f * (to.y – from.y – height) / Mathf.Pow(0.67f * time, 2);
while (t <= time)
{
float deltaTime = Time.deltaTime;
float deltaSx = Vx * deltaTime;
Vy -= A2 * deltaTime;
float deltaSy = Vy * deltaTime;
trans.localPosition = new Vector3(trans.localPosition.x + deltaSx, trans.localPosition.y + deltaSy);
t += deltaTime;
yield return null;
}
}
Enjoy this article?
Consider subscribing to our RSS feed!
No Comments
No comments yet.