I'm currently trying to improve a floating script to enable a boat to roll on waves. In it's initial state the boat only moves based on the height of the water at it's position.
To improve this I am trying to rotate the boat on X and Z based on the height of the water at the front, back , left and right. I'm sure this involves some application of trig however I am not familiar enough with how to apply this. In addition the rotation at 2 angles at once may complicate it further.
void FixedUpdate()
{
//Original script: Currently determines height of boat
if ( Water == null ) return;
height = Water.GetHeightAtPoint( transform.position );
transform.position = new Vector3( transform.position.x, height, transform.position.z );
//
//New code to make boat roll with waves.
//Position height of water will be determined from
shipPosFront = new Vector3(transform.position.x, transform.position.y, transform.position.z + shipSizeZ);
shipPosBack = new Vector3(transform.position.x, transform.position.y, transform.position.z - shipSizeZ);
shipPosRight = new Vector3(transform.position.x + shipSizeX, transform.position.y, transform.position.z);
shipPosLeft = new Vector3(transform.position.x - shipSizeX, transform.position.y, transform.position.z);
shipHeightFront = Water.GetHeightAtPoint(shipPosFront);
shipHeightBack = Water.GetHeightAtPoint(shipPosBack);
shipHeightRight = Water.GetHeightAtPoint(shipPosRight);
shipHeightLeft = Water.GetHeightAtPoint(shipPosLeft);
//How do we convert these 4 heights to x/z rotation?
}
The basic floating script came with the Hydroform asset on the Unity store.
↧