How to rotate object back and forth from one rotation to another?
I figured I could use something like an IENumerator but I want the object to rotate back and forth from two set positions every x seconds. Thanks for any help
Answer by Segy · Apr 30, 2016 at 05:41 PM
I know this answer is too late for you, but hope this helps anyone else with the same problem in the future!
public float speed = 2f;
public float maxRotation = 45f;
void Update()
{
transform.rotation = Quaternion.Euler(maxRotation * Mathf.Sin(Time.time * speed), 0f, 0f);
}
Thanks for the reply! Even though it's an old post this helped me out a ton.
what about have it rotate a little slower every time it hits the end?
Can't believe it, I searched for it for an hour with many complicated answers. This is just amazing. Thanks
I recommend changing transform.rotation to transform.localRotation, Allowing you to change the parent objects rotation, while keeping the same movement. I rarely recommend changing rotation directly.
Answer by VoltzStudios · Dec 15, 2016 at 02:03 AM
The rotation thing doesn't rotate, it grows and shrinks the object.
Hello, this is because the example above applies rotation to the x Axis. It would be helpful to read up about rotational maths if you are not able to apply this to suit your needs. http://mathworld.wolfram.com/Rotation$$anonymous$$atrix.html That.. or ("I don't wanna be That guy but...) check the methods you intend to use before actually applying them in code..
To rotate a 2D-GameObject like a sprite you should rotate along the z-axis: Simply move the rotation-value from the first to the last position:
transform.rotation = Quaternion.Euler(0f, 0f, maxRotation $$anonymous$$athf.Sin(Time.time speed));