Combining Rotation = and Rotate()
Hello.
I'm trying to have an object look in the direction of its velocity, but also spin on its Z axis.
void Update ()
{
this.transform.Rotate(Vector3.forward, (500 * Time.deltaTime), Space.Self);
this.transform.rotation = Quaternion.LookRotation(mVelocity);
}
The problem is that the two functions over-ride each other. If I comment out the .rotation function, the object spins properly but doesn't look in the right direction. If I leave the rotation function in code, the oject points in the right direction, but doesn't rotate on its axis.
Any suggestions on making these two play nicely together?
thanks David
Answer by toromano · Apr 25, 2016 at 01:08 PM
This might work:
void Update()
{
var rot = Quaternion.LookRotation(mVelocity);
var spin = Quaternion.AngleAxis(500 * Time.deltaTime, transform.forward);
transform.rotation = rot * spin;
}
Nope,, sorry tried that.
Took me a second to figure out what var was though.. I used;
Quaternion rot = Quaternion.LookRotation(mVelocity);
Quaternion spin = Quaternion.AngleAxis(500 * Time.deltaTime, transform.forward);
this.transform.rotation = rot * spin;
But still, all that happens is that the object sort of jitters in place.
I thought you could just add them but apparently you can't add a Quanternion.
thanks
Could you try :
Quaternion rot = Quaternion.LookRotation(mVelocity,transform.up);
And if it does not work again, changing rot spin order to spin rot might work.
Hope that helps
Hmmm nope, not that either..sorry.
I also tried this as a lark;
Quaternion spin = Quaternion.AngleAxis(this.transform.rotation.eulerAngles.z + (90 * Time.deltaTime), transform.forward);
but all that happens is that the object gets to 90deg and flips overitself and points the other direction... very strange.