That worked awesome!!!
BUT… Now I face a new problem… Ok, a couple.
Here are the motions I’m trying to accomplish
- Bobbing up and down
- Rocking front to back
- Rocking side to side.
It’s what ships do.
I split each motion out into three different functions in one script.
– BobUpAndDown
– RockFrontToBack
– RockSideToSide
And called them from the Update function. Individually, they all work as I had hoped. Furthermore, I can combine BobUpAndDown together with a rocking motion. It just always works, I guess because it’s not adjusting the rotation. So, no matter what I do in RockFrontToBack or RockSideToSide, it does not mess up the bobbing motion.
So far so good… Where my problem is, is that I CANNOT combine the RockFrontToBack and RockSideToSide. And now that I think about it, it kinda makes since. Within the same frame I’m trying to adjust the rotation, but the second function will always win out.
SO… I tried to put in a Boolean switch in my Update… Something like this which would toggle each frame between which direction to rock.
if( sideToSide )
RockSideToSide( );
else
RockFrontToBack( );
sideToSide = !sideToSide;
Well, that created a visual mess. Just didn’t work… So, I thought, well… Maybe if I split the functions out into individual script files and attach each script to a ship, MAYBE that will work. It didn’t. I got the same as before, the second script just overwrites the first script calculation…
For reference, here is the current script I’m using.
using System;
using UnityEngine;
public class RockShip : MonoBehaviour
{
float originalY;
public float bobbingMultiplier = .5f;
public float rollMultiplier = .4f;
private Quaternion startRotation;
void Start()
{
originalY = this.transform.position.y;
startRotation = transform.rotation;
}
void Update()
{
BobUpAndDown( );
RollSideToSide( );
RollFrontToBack( );
}
void BobUpAndDown( )
{
transform.position = new Vector3(transform.position.x,
originalY + ((float)Math.Sin(Time.time) * bobbingMultiplier),
transform.position.z);
}
void RollSideToSide( )
{
float f = Mathf.Sin( Time.time * rollMultiplier ) * 10f;
transform.rotation = startRotation * Quaternion.AngleAxis( f, Vector3.forward );
}
void RollFrontToBack()
{
float f = Mathf.Sin(Time.time * rollMultiplier) * 2f;
transform.rotation = startRotation * Quaternion.AngleAxis(f, Vector3.left);
}
}
I had put both rotations into one single function with the hopes of trying to use a temporary “Quaternion” variable. The thought was that I could modify the temp quaternion’s x value with one rotation, and then modify the temp quaternion’s z with the other rotation, then apply the entire rotation to the game transform.
That didn’t work out so well either… Not positive on what I was doing wrong, but in theory it seems like it would work. I suspect I was not supposed to be modifying the x and y directly. But, none the less.
So, If anyone of you brilliant minded folks can throw me a bone, it will be greatly appreciated!! 
Having two of the three is pretty damn cool especially in VR.
OH YEAH… Then there was the other problem.
I have several ships all aligned up. How do I randomize the motion of the scripts? Right now they all start at the exact same rotation, same bobbing level. I don’t really want to put a time delay when they start, rather it would be nice to inject some kind of seed in the Mathf.Sin functions. Something to that affect anyway.
I had thought that I could start each ship with a different rotation. Problem is, Sin does not really take into consideration that the ship is already tilting say 10 degrees, it will just extend to the side that much further, and then 10 degrees, would be top dead center for the sign. It just didn’t work.