- Home /
Simple projectile motion with wind
Hi, I am a programmer myself but trigonometry is not my favourite area if things get complicated. I made a prototype that calculates the launch degrees of a thrown ball with a changing speed that is affected by changing wind(horizontal) and changing gravity(vertical), that the ball going from specific destination (character position)A to (target position)B. Then throws a projectile starting from the same angle of the character and affected by the same speed, wind, gravity. So when the player turns to the first calculated position, the projectile reaches the destination B.
Here is the code for the prototype:
Calculation.cs :
public class Calculation : MonoBehaviour
{
public float speed;
public float verticleangle;
public float horizontalangle;
public float windangle;
public float gravity;
public float wind;
public Transform target;
public GameObject arrow;
void Update ()
{
// Calculate distance to target
float target_Distance = Vector3.Distance(transform.position, target.position);
// Calculate the verticle angle needed
verticleangle = Mathf.Asin(target_Distance * gravity / speed) / 2 * Mathf.Rad2Deg;
// Calculate the horizontal angle needed
windangle = Mathf.Asin(target_Distance * wind / speed) / 2 * Mathf.Rad2Deg;
if (float.IsNaN(verticleangle))
print("outofrange");
if (Input.GetKeyDown(KeyCode.Space))
fire();
}
void fire()
{
Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
GameObject temparrow = Instantiate(arrow, transform.position, transform.rotation) as GameObject;
StartCoroutine(temparrow.GetComponent<Throw>().throwaction(speed, verticleangle, windangle, gravity, wind, rotation ));
}
}
Throw.cs
public class Throw : MonoBehaviour
{
public IEnumerator throwaction(float speed, float verticleangle, float windangle, float gravity, float wind, Quaternion orgrot)
{
//Extract the X Y componenent of the velocity
float Vx = Mathf.Sqrt(speed) * Mathf.Cos(verticleangle * Mathf.Deg2Rad);
float Vy = Mathf.Sqrt(speed) * Mathf.Sin(verticleangle * Mathf.Deg2Rad);
float Vz = Mathf.Sqrt(speed) * Mathf.Sin(windangle * Mathf.Deg2Rad);
Quaternion newrot = transform.rotation;
float elapse_time = 0;
Vector3 oldpos;
while (true)
{
elapse_time += Time.deltaTime;
transform.rotation = newrot;
oldpos = transform.position;
//Move the projectile
transform.position += Vx * Time.deltaTime * ( transform.forward);
transform.position += ( - (gravity * elapse_time)) * Time.deltaTime * (transform.up);
transform.position += ( - (wind * elapse_time)) * Time.deltaTime * (-transform.right);
//Rotate the player
transform.rotation = Quaternion.LookRotation(transform.position - oldpos);
if (elapse_time > 8)
{
Destroy(gameObject);
yield break;
}
yield return null;
}
}
}
Can someone tell me what is going on wrong in this code?
Here is the working prototype as web build to try out:
Here is my prototype that I am using:
Thanks
Your answer
Follow this Question
Related Questions
A job task : Projectile Motion 0 Answers
Projectile motion with angle and force in one direction 1 Answer
Finding angle of angular motion 1 Answer
Projectile(Arrow) angle during flight is not realistic 1 Answer
bullet realistic penetration 0 Answers