Missile Trajectory

Hi All,

First I would like to say I’ve got it rotating to a direction, but I’m not sure if this is the correct way to do it.

I’m cheating a parabolic path, with the following code:

  private void Update()
  {
   if (mIsMoving == true)
   {
 mIncrementor += 0.02f;
  Vector3 mPosition = Vector3.Lerp(mStartPosition, mEndPosition, mIncrementor);
    mPosition.y += mForceUpwards * Mathf.Sin(Mathf.Clamp01(mIncrementor) * Mathf.PI);
    transform.position = mPosition;

  Vector3 relativePos = mPosition - mOldPosition;
    Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up) * Quaternion.Euler(-90, 0, 0);
    transform.rotation = rotation;

    mOldPosition = mPosition;
   }
 }

First, as this is a prototype capsule I have to rotate it by -90 e.g Quaternion.Euler(-90, 0, 0).

so basically what I’ve done is every update subtracting the mOldPosition for the mPosition so I can calculate the rotation.

So is there a better way to do this without keeping the Old Position of the object.

Cheers

Not really, this is a pretty classic and common way of setting rotation.

mIncrementor should probably increase by Time.deltaTime and not 0.02 though, so as to make its speed not dependent on framerate.

@StarManta

Are I’m glad I got that correct, Yeah I did notice that was wrong from an old script I had, and set about updating my code.

AS I was figuring out I needed some sort of speed variable in there

I came up with two different Idea’s:

float mTime = (Time.time - mStartTime) * mSpeed;
 Vector3 mPosition = Vector3.Lerp(mStartPosition, mEndPosition, mTime);
 mPosition.y += mForceUpwards * Mathf.Sin(mTime * Mathf.PI);
 transform.position = mPosition;

or

  mIncrementor = Mathf.Clamp01(mIncrementor + Time.deltaTime * mSpeed);
  Vector3 mPosition = Vector3.Lerp(mStartPosition, mEndPosition, mIncrementor);
  mPosition.y += mForceUpwards * Mathf.Sin(mIncrementor * Mathf.PI);
   transform.position = mPosition;

now if I use the first snippet of code the following will never be met.

if (transform.position == mEndPosition)
{
 mIsMoving = false;
 // destroy object etc
}

I have to use a Distance check

if (Vector3.Distance(transform.position, mEndPosition) <= 0.1F)
  {
   //  mIsMoving = false;
   // destroy object etc
   }

but the second snippet works fine with the clamping.

for some reason once I introduced time.deltatime things got worse and did not work;

Cheers