after a lot of experimenting over the last couple of weeks, i've nearly finished the lever manager. the following post is related more to the code than level design...
1st image is the whole of my lever data script. these would be attached to each of the lever prefabs.
level designers could replace the meshes and textures with different versions if they wanted, as long as the prefabs followed the hierarchical structure of the ones that i'll be releasing, hopefully in the near future. alternatively coders could edit the scripts to suit their own designs.
lines 17-20 are what we're focusing on here, as they are used to store the reference to the prefab's pivot, the start and end rotations, as well as the target rotation. the latter toggles between start and end when the lever reaches the target rotation.
2nd image is part of my start function of the lever manager script. the various managers are attached to an empty game object in the root of the scene's hierarchy window. there is only one manager of each type.
lines 89-92 are assigning the initial values to the data script's variables. the most important, and difficult for me to figure out, was line 91. which is the end rotation.
to my surprise, that line didn't require at this stage, any reference to the pivot. instead it's using my inspector variable "end angle x", which defaults to 90 degrees. it also references the vertical "y" axis of the prefab parent, ie the lever base.
3rd image is part of the "fixed update" function (ie physics update) of the manager script.
line 126 is interpolating between the current rotation (taken from the pivot each frame) and the target rotation.
i was surprised by how high the "250" value ended up being, as that determines speed per second. normally i move or rotate objects with values between 5-25. i guess it has something to do with converting that value to degrees, but it's still weird looking. any less and it was moving too slow in my opinion.
that value could be exposed in the inspector for the lever designers, but for now i'm leaving it hard coded.
another thing which i'm still trying to comprehend, is the beginning of that line has "leverData.pivot.rotation = " which means it's not just storing the interpolated value in the data script, but also apparently applying it to the actual pivot, rather than a reference to it. i assume this has something to do with "reference types vs value types" although admittedly i'm not good at distinguishing the former.
in the past i would have done those separately. it was actually a typo that i wrote it like this, but it produced good results so i kept it.
...
a longer version of the lever manager script that i got working a couple of days ago, took a slightly different approach. in that instead of interpolating between start and end quaternions, i broke it down to interpolating a "current angle x" floating point variable, similar to the "end angle x" previously mentioned.
i then used a built in quaternion function to construct the current rotation from the "x, y, z" and applied that to the pivot.
the reason i had tried that was i wanted more control over each frame. so in effect constraining the "y" and "z" axis so there was no unwanted drift on them. turns out i didn't need to do that but it's good to know we can if need be.
in summery, it was a lot of hard work to get the levers to behave as i wanted, although i learned several very useful things in the process... there's even a couple of interpolate functions that i tested, that have acceleration and deceleration in their movement. in the end, i opted for a constant speed for these levers as that seemed more natural.