Throw Rigibody2D cannon to a 2D target (with gravity), reach in time, how to calculate Addforce ? Please & Thx
This is the simulate picture:
And here is my testing Script.. may have so many logic errors..
void Fire_CannonInSeconds(GameObject oBullet, float fTotalTime) {
if (oBullet==null)
return;
// Total X range
float fXRange = Mathf.Abs( trTarget.localPosition.x - oBullet.transform.localPosition.x );
//
float forceRight = oBullet.rigidbody2D.mass * (fXRange / fTotalTime);
// SET: the reach Top point time = Total * 2 / 3..
float fToTopTime = fTotalTime * 2f / 3f;
// SET: Top Point
float fTop = Mathf.Max( trTarget.localPosition.y, oBullet.transform.localPosition.y ) + 0.2f;
// calculate Bullet to top point movement range
float fMoveY = fTop - oBullet.transform.localPosition.y;
// here ... i don't know how to ... so...
float fTot = Physics2D.gravity.magnitude * oBullet.rigidbody2D.mass * (fMoveY / (fToTopTime / Time.fixedDeltaTime));
// seems not work here..
float forceUp = Physics2D.gravity.magnitude + fTot ;
oBullet.SetActive(true);
oBullet.rigidbody2D.AddForce (Vector3.up * forceUp);
oBullet.rigidbody2D.AddForce (Vector3.right * forceRight);
}
Thank you very much Q_Q
throw2dtotargetintime.png
(5.5 kB)
Comment
Your answer
Follow this Question
Related Questions
HOW TO ADD GRAVITY TO A OBJECT? 2 Answers
ForceMode2D.Impulse problem 2 Answers
Gravity seems "slow". 5 Answers
Why is my momentum not carrying over to the X axis? 0 Answers
How to make sphere a cricket ball? 1 Answer