Martin Dawson
Martin Dawson

Reputation: 7656

Untiy Make an object rotate along axis its moving on

I want to make an object (let's say a cube), rotate at a steady rate on the same axis that's it's moving on. So if it changes direction from X to Z then the rotation would lerp from X axis into the Z axis and then continue rotating on Z axis.

How would I achieve this? Here's what I have at the moment, the cube just rotates on the z axis back and forth within a certain degrees.

public float Angle;
public float Period;

void Update()
{
    Animate();
}

void Animate()
{
    _time = _time + Time.deltaTime;
    float phase = Mathf.Sin(_time / Period);
    transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, phase * Angle));
}

Upvotes: 1

Views: 91

Answers (1)

Fattie
Fattie

Reputation: 12582

Just use

RotateAround

Note generally NEVER use Quaternion for any reason.

there are 1000s of questions on using RotateAround so just google. In your case it sounds like you'll be changing (lerping, whatever) the axis of rotation itself.

Upvotes: 2

Related Questions