BGP Week 5 – Calibrating Rotations

While the rotating shroom was something I started on some weeks before, this was the week when the final issues were fixed up as the final version would work, and as such this seems like a good time to explain how it works.

While not massively complex, the curse of this platform that took time is that for rotation, Unity relies on quaternions, which is not exactly the easiest to understand. However, eventually I realized all that was needed was a relatively simple line of code need: transform.rotation = Quaternion.AngleAxis(spin, Vector3.up) * transform.rotation;

With this, all modifications to how the rotations occur, such as speed and direction, were modified in the spin variable, keeping the rest untouched. To this, I added a Clamp function that sets a maximum and minimum rotation limit as public floats, as well as a public bool to turn the limitation on or off, as needed. And as a final touch, a bool which, if turned on, would make the rotation start in reverse.

If the rotation reaches the set clamp limits with the function on, spin is set to its opposite, causing it to reverse and go back until hitting the opposite limit, repeating the process to send the rotation back and forth. As one of the later touches, I changed how the limits worked so that they adjusted to any rotation done to the object in the editor, to make it more designer friendly.

About Stefan Ekdahl

2014  Programming