I am currently working on a script for simple water buoyancy for a college project and I almost have everything working as I want it to, however after using Add force at position (which gives the exact effect I am after) To rotate around the X and Z axis they also seem to be affecting the Y axis rotation. I don’t know if this is un-expected behavior or not as I have never used Add force at position before, if it isn’t un-expected then how do I fix it, if it is un-expected, what is causing it?
The code below is what I have so far, it is applied to all Water objects and affects any object it comes into contact with that has a rigid-body after reading each objects variables from varass (posted below)
WaterScript
var colliders = new Array();
//Add/Remove any objects to the array that enter/leave the water
function OnTriggerEnter (other : Collider)
{
colliders.Add(other.gameObject);
}
function OnTriggerExit (other : Collider)
{
other.rigidbody.drag=0;
other.rigidbody.angularDrag=0.05;
colliders.remove(other.gameObject);
}
function FixedUpdate()
{
for(var hit in colliders)
{
if(hit.rigidbody)
{
//Set the Various Stats, calculated and read from varass, to each object
var waterLevel = transform.position.y;
var floatStats = hit.collider.gameObject.GetComponent("varass") as varass;
var floatForce = floatStats.bouyancy+((waterLevel-hit.transform.position.y)*5);
var returnSpeedX=((floatStats.IXPos-hit.rigidbody.rotation.x));
var returnSpeedZ=((floatStats.IZPos-hit.rigidbody.rotation.z));
hit.rigidbody.drag=1;
hit.rigidbody.angularDrag=1;
//Apply current and float force as well as 'righting' forces
hit.rigidbody.AddForce (floatStats.xCurrent, floatForce, floatStats.zCurrent);
hit.rigidbody.AddForceAtPosition(Vector3(returnSpeedX,0,0), Vector3(transform.position.x, transform.position.y, transform.position.z));
hit.rigidbody.AddForceAtPosition(Vector3(0,0,returnSpeedZ), Vector3(transform.position.x, transform.position.y, transform.position.z));
}
}
}
varass
var bouyancy=50;
var IXPos=0;
var IYPos=0;
var IZPos=0;
Thanks, Tom.