Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xmediagrafx · Oct 19, 2010 at 01:26 AM · iphonespeedgui-button

boost speed button script issue...

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 &gt; 0 )
 {
     isBoosted = true;
 }

 if (isBoosted) {
 speed = boostSpeed;

} else { speed = normalSpeed; }

 /*if ( thisTransform.position.y &lt; 0 )
 {
     var splashClone = Instantiate( splash, thisTransform.position, thisTransform.rotation );
     Destroy( gameObject );
     gameObjectGUI.SendMessage( "crash" ); 
     Debug.Log( "Splash" );
 }*/

}

function wrapAngle( angleToWrap : float ) { return 180 - angleToWrap;
}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xmediagrafx · Oct 20, 2010 at 01:00 AM 0
Share

after further testing, not losing control of plane, just boost not disengaging which is causing plane to fly crazy, haha:) getting there..

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Murcho · Oct 19, 2010 at 02:30 AM

You seem to be setting isBoosted to false in every OnGUI call, which means by the time you query it in the FixedUpdate loop, it is always going to be set to false.

You should move the initialization of the isBoosted variable to your start function and remove it from the OnGUI function.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xmediagrafx · Oct 20, 2010 at 12:51 AM 0
Share

i rewrote the script and combined into one script, still having issues as you can read above. but i achieved boost:) just need to clean up script and work out bugs...thanks for your reply!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to accelerate and improve the rendering on iPhone ? 0 Answers

Frames per second capped at 30 for iPhone? 1 Answer

Is "PlayerPrefs" (Get/Set) slow ? 2 Answers

How to calculate swipe speed on iOS 2 Answers

iPhone - How to calculate a decent touch swipe speed? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges