rewrote script, this works (kind of)...only issue is that it works, but doesnt disengage, and also seems to interfere with control of plane?? I am sure this can be cleaned up to work better, ideas?? I am new to scripting, but learning fast, thanks for the help!!
private var isBoosted : boolean = false;
private var normalSpeed : int = 1000;
private var boostSpeed : int = 2000;
var speed : int;
var gameObjectGUI : Transform = null;
var explosion : GameObject;
var splash : GameObject;
private var forceWingLeft : Transform;
private var forceWingRight : Transform;
private var forceTailLeft : Transform;
private var forceTailRight : Transform;
private var forceNoseLeft : Transform;
private var forceNoseRight : Transform;
private var forceThrust : Transform;
private var thisTransform : Transform;
private var thisRigidbody : Rigidbody;
private var thisGravity : Vector3;
private var myPitch : float;
private var calPitch : float;
private var offsetPitch : float;
private var calibrationZ : float;
function FixedUpdate()
{
if ( iPhoneInput.touchCount > 0 )
setCalibration();
var accel : Vector3 = iPhoneInput.acceleration;
if ( calibrationZ > 0 )
myPitch = -Mathf.Atan2( accel.x, accel.z ) * Mathf.Rad2Deg;
else
myPitch = Mathf.Atan2( accel.x, -accel.z ) * Mathf.Rad2Deg;
calPitch = ( wrapAngle( offsetPitch ) - wrapAngle( myPitch ) ) / 360;
thisRigidbody.AddForceAtPosition( thisTransform.forward * speed, forceThrust.position );
//Get the angle between our velocity and the front of the wing
var angleOfIncidence = Vector3.Angle( thisTransform.forward, thisRigidbody.velocity );
//Taking the Sin of the Angle of Incidence will yield 0 at 0 degrees difference
//and 1 (representing full friction at 90 degrees). This is what changes the direction
//of the plane's velocity vector.
var wingFriction = 0.1 + Mathf.Abs( Mathf.Sin( angleOfIncidence ) );
//Add a small amount to the acceleromter values if they are equal to zero
var accelX = calPitch;
if ( Mathf.Abs( accelX ) < 0.02 )
accelX = 0.0001;
var accelY = accel.y;
if ( Mathf.Abs( accelY ) < 0.1 )
accelY = 0.0001;
var relativeLeftWingPosition : Vector3 = thisTransform.position - forceWingLeft.position;
var relativeRightWingPosition : Vector3 = thisTransform.position - forceWingRight.position;
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceTailLeft.position ) * 0.1 ) + ( ( -thisTransform.up * 5 * -accelX ) * wingFriction ), forceTailLeft.position );
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceTailRight.position ) * 0.1 ) + ( ( -thisTransform.up * 5 * -accelX ) * wingFriction ), forceTailRight.position );
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceNoseLeft.position ) * 0.1 ), forceNoseLeft.position );
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceNoseRight.position ) * 0.1 ), forceNoseRight.position );
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceWingLeft.position ) * 0.25 ) + ( ( thisTransform.up * 2 * -accelY ) * wingFriction ) - thisTransform.forward * relativeLeftWingPosition.y * thisRigidbody.velocity.magnitude * 0.0025, forceWingLeft.position );
thisRigidbody.AddForceAtPosition( thisGravity + ( -thisRigidbody.GetPointVelocity( forceWingRight.position ) * 0.25 ) + ( ( thisTransform.up * 2 * accelY ) * wingFriction ) - thisTransform.forward * relativeRightWingPosition.y * thisRigidbody.velocity.magnitude * 0.0025, forceWingRight.position );
audio.pitch = 1.0 - Mathf.Clamp( thisRigidbody.velocity.y / 100, -1, 1 );
}
function handleRaycasts( theHit : RaycastHit )
{
switch( theHit.transform.name )
{
case "coin":
Destroy( theHit.transform.gameObject );
Debug.Log( "Collected a coin!" );
break;
/*case "water":
var splashClone = Instantiate( splash, thisTransform.position, thisTransform.rotation );
Destroy( gameObject );
//gameObjectGUI.SendMessage( "crash" );
var script : crash = gameObjectGUI.gameObject.GetComponent( crash );
script.crash();
Debug.Log( "Splash" );
break;*/
default:
var explosionClone = Instantiate( explosion, thisTransform.position, thisTransform.rotation );
Destroy( gameObject );
gameObjectGUI.SendMessage( "crash" );
Debug.Log( "Crash" );
break;
}
}
function setCalibration()
{
var accel : Vector3 = iPhoneInput.acceleration;
calibrationZ = accel.z;
if ( calibrationZ > 0 )
{
offsetPitch = -Mathf.Atan2( accel.x, accel.z ) * Mathf.Rad2Deg;
offsetYaw = -Mathf.Atan2( accel.y, accel.z ) * Mathf.Rad2Deg;
}
else
{
offsetPitch = Mathf.Atan2( accel.x, -accel.z ) * Mathf.Rad2Deg;
offsetYaw = Mathf.Atan2( accel.y, -accel.z ) * Mathf.Rad2Deg;
}
}
function Start()
{
forceWingLeft = transform.Find( "forceWingLeft" );
forceWingRight = transform.Find( "forceWingRight" );
forceTailLeft = transform.Find( "forceTailLeft" );
forceTailRight = transform.Find( "forceTailRight" );
forceNoseLeft = transform.Find( "forceNoseLeft" );
forceNoseRight = transform.Find( "forceNoseRight" );
forceThrust = transform.Find( "forceThrust" );
thisTransform = transform;
thisRigidbody = rigidbody;
thisGravity = Vector3( 0, -9.8, 0 );
setCalibration();
}
function Update()
{
var hit : RaycastHit;
var layerMask = 1 << 2;
layerMask = ~layerMask;
if ( Physics.Raycast( thisTransform.position, thisTransform.forward, hit, 10, layerMask ) )
handleRaycasts( hit );
if ( Physics.Raycast( thisTransform.position, -thisTransform.forward, hit, 10, layerMask ) )
handleRaycasts( hit );
if ( Physics.Raycast( thisTransform.position, thisTransform.up, hit, 5, layerMask ) )
handleRaycasts( hit );
if ( Physics.Raycast( thisTransform.position, -thisTransform.up, hit, 5, layerMask ) )
handleRaycasts( hit );
if ( Physics.Raycast( thisTransform.position, thisTransform.right, hit, 10, layerMask ) )
handleRaycasts( hit );
if ( Physics.Raycast( thisTransform.position, -thisTransform.right, hit, 10, layerMask ) )
handleRaycasts( hit );
if ( iPhoneInput.touchCount > 0 )
{
isBoosted = true;
}
if (isBoosted) {
speed = boostSpeed;
}
else {
speed = normalSpeed;
}
/*if ( thisTransform.position.y < 0 )
{
var splashClone = Instantiate( splash, thisTransform.position, thisTransform.rotation );
Destroy( gameObject );
gameObjectGUI.SendMessage( "crash" );
Debug.Log( "Splash" );
}*/
}
function wrapAngle( angleToWrap : float )
{
return 180 - angleToWrap;
}