Rotate from StartPostion to EndPostion while moving

Here is what I’m trying to do, I have a gameobject, that is off screen what I want to do is move the gameobject into view at position Vector3.zero, while this is going on I want to rotate the object as may times as I want(I was messing about just after poting and I figured out only a single rotation) while its moving, but It MUST finish(or there about’s I can rest rotation when move finishes) at zero rotation.

My movement is working fine, but I just cannot workout the rotation.

My setting up of the Object is as follows:

I put 1080 on the Z plan so I could get it to rotate three full rotations, but I forgot it’s 180 or -180.

GameObject mLetter = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Letter"));
  mLetter.name = "Letter";
  mLetter.transform.position = new Vector3(10F, 0F, 0F);
  mLetter.transform.eulerAngles = new Vector3(0F, 0F, 1080F);
  LetterComponent mLetterComponent = mLetter.AddComponent<LetterComponent>();
  mLetterComponent.enabled = false;
  mLetterComponent.Reached = 0.5F;
  mLetterComponent.Speed = 1.0F;
  mLetterComponent.MoveTo = new Vector3(0F, 0F, 0F);
  mLetterComponent.enabled = true;

My movement code and rotation:

public virtual void Awake()
{
mIsReached = false;
mReached = 0;
mMoveFrom = this.transform.position;
mSpeed = 1.0F;
mMoveTo = Vector3.zero;

mStartrot = this.transform.rotation ;
mEndrot = new Quaternion(0F, 0F, 0F, 0F);
}

public virtual void Update()
{
mTime += mSpeed * Time.deltaTime;

if (mTime <= 1.0)
   {
    transform.position = Vector3.Lerp(mMoveFrom, mMoveTo, mTime);
    transform.rotation = Quaternion.Lerp(mStartrot, mEndrot, mTime);
   }
else
       {
        transform.position = mMoveTo;
        OnActionCompleted(new ActionCompletedEventArgs());
       }

if (mIsReached == false && mReached > 0)
  if (mTime >= mReached)
    {
     OnMovementReached(new MovementReachedEventArgs());
     mIsReached = true;
    }
}

Use Lerp on euler angles instead of quaternions.

For example:

//N - number of full rotations
int N = 3;
float startAngle = N * 360.0f;
float endAngle = 0;

float currentAngle = Mathf.Lerp(startAngle, endAngle, mTime);
transform.rotation = Quaternion.Euler(0, 0, currentAngle);

Quaternion.Lerp interpolates between two quaternions but Quaternion.Euler(0, 0, 0) equals Quaternion.Euler(0, 0, N * 360.0f) so that is why it didn’t work.

Hi DWit,

Thanks it came to me last night, later after I did the post, that I should be using the distance, I had it in there but took it out for the post, here is what I come up with, the base class already does the movement, so I inherited from that, and allowed the time variable to be passed though, then it was a matter of hocking up the rotation code.

public class LetterComponent : ActionComponent 
{
private Single mCompleteDistance;
private float mdivision;

  void Start()
  {
   mCompleteDistance = Vector3.Distance(this.transform.position, base.getMoveTo);
   mdivision = (360 / mCompleteDistance);
  }

  public override void Update()
  {
   base.Update();

   if (base.Time <= 1.0)
     {
      float mDistance = Vector3.Distance(transform.position, base.getMoveTo);
      transform.rotation = Quaternion.Euler(0.0F, 0.0F, -(mdivision * mDistance)); 
     }
   else
     transform.rotation = Quaternion.Euler(0F, 0F, 0F);      
  }
}

Its actually for a splash screen I’m designing, I’ve been using the Unity Splash Screen(I can change this as I have the plus account), but I’m not really happy with the result, it looks ok on some devices but others it looks to big or too small, my Samsung 10.1 or My Nexus 6 are the extremes, so I set about on Sunday creating my own, I used some code I had developed for my dialog moving into view.

With a bit of extra code, I’ve now got the letters “G A M E” rolling into view, now I’ll put the rest of the logo together today and I hope it work on different sized machines.

Here is my new Splash Screen In action, it does not use the mecanim system, it’s purely in code, a couple of hundred lines of code, for the animation sequence, you simply bolt on what you want e.g I want to move bolt on the movement component, I want to Rotate bolt on the Rotate Component or both, you can even get a callback when it reaches a certain point, to start another movement like I do, or even at the end of the sequence.

2882988--211711--SplashScreen.gif
What do you think??