- Home /
projectile formula
hello, i am developing like link text game in unity2d. i want to create the same effect of generating path(white circles). and make the bullet follow the same path. I have searched about this implementation and found that it is possible using projectile. I referred link text but didn't find any formula to generate path. can anybody help me how to implement this?
Answer by Ashish Dwivedi · Feb 15, 2014 at 06:27 AM
I already replied to this question. You can see here. How ever I'm posting here also for your convenience.
Vector3 [] GetTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
{
float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
float fTime = 0;
Vector3 [] pos = new Vector3[20];
int iNumberOfPoints = 10;
fTime += 0.1f;
for (int i = 0 ; i < iNumberOfPoints ; i++)
{
float x = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
float y = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
pos[i] = new Vector3(pStartPosition.x + x , pStartPosition.y + y ,0);
fTime += 0.1f;
}
return pos;
}
Here you need to pass start position of projectile and initial velocity and method will return the number of points on the projectile trajectory.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy shooting at player Unity2D 1 Answer
Calculate parabolic initial velocity and angle? 0 Answers
Precise projectile launching in 3d world 0 Answers
Trajectory Prediction with Particle Systems 1 Answer
Move projectile in a specific direction 0 Answers