- Home /
 
How to detect object go up or down?
Hi I have an airplane object and I want to when my airplane go up speed increase and when go down speed decrease or some times when it change direction more than a number (30 degree or ...) to left or right the speed decrease, but don't know how can detect the direction of moving ? I want to do this work just by code. thanks
Going to need a bit more info than that.. Have you already written code? 
Do you want the airplane to be controlled by the player or do you want it to fly on it's own? 
Are you going for the unity physics approach with a rigidbody or are you planning on calculating it all separately? 
If you have code please post it. If you are using a rigidbody you can use rigidbody.velocity to detect the direction the object is moving in :)
     var UpForce : float;
 // On air Speed    
     var $$anonymous$$axSpeed : float = 200;
     var $$anonymous$$inSpeed : float = 30;
     var Speed : float = 60;
     var Acceleration : float = 50;
     var GroundSensor : boolean = true;
     var RealSpeed : float;
     
 function Update () {
 
 //======================== On ground ================================//
 //UpForce help to take off easily
 
 if(GroundSensor){
     
     UpForce = rigidbody.velocity.z;
     
     Debug.Log("force :" && UpForce);
     if(UpForce > 30 ){
         rigidbody.velocity.y = UpForce;
     }    
 }
 //========================= On Air Control ==========================//
 
     if(!GroundSensor){    
 //forward force 
 
         rigidbody.velocity = transform.forward * Speed;
     
 // speed 
     
     Speed = $$anonymous$$athf.Clamp(Speed,$$anonymous$$inSpeed,$$anonymous$$axSpeed); 
     
     if(Speed <= $$anonymous$$inSpeed){
         Speed = $$anonymous$$inSpeed;
     }
     if(Speed >= $$anonymous$$axSpeed){
         Speed = $$anonymous$$axSpeed;
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.B)){
         Speed = Speed + 0.25;
     }
     else{
     if(Speed >= 170){
         Speed = Speed - 0.25;
     }
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.N)){
         Speed = Speed - 0.25;
     }
     
     // moving direction manager
 //Roll    
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){
         transform.Rotate(0,0,Acceleration*Time.deltaTime);
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
         transform.Rotate(0,0,-Acceleration*Time.deltaTime);
     }
 //$$anonymous$$ch    
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)){
         transform.Rotate(-Acceleration*Time.deltaTime,0,0);
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)){
         transform.Rotate(Acceleration*Time.deltaTime,0,0);
     }
 //Yaw
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q)){
         transform.Rotate(0,-Acceleration*Time.deltaTime,0);
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E)){
         transform.Rotate(0,Acceleration*Time.deltaTime,0);
     }
 }
 //======================= Realism manager =========================//
         
     //Control Acceleration with Speed
     
     Acceleration = 1/Speed*1200;
     
     Acceleration = $$anonymous$$athf.Clamp(Acceleration,25,45);
     
     //Control Gravity with Speed
     //$$anonymous$$ Gravity with max Speed
     //max Gravity with $$anonymous$$ Speed 
     //Can work with Addforce
     
     // Control Speed with $$anonymous$$ch
     // detect rotation value and decide
     
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)){
         if(!Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
             Debug.Log("yes!!");
         }    
     }
     
 }
 //======================= sensor for ground =======================//
 
 function OnCollisionEnter(collision : Collision) {
 
 // "Ground" is Ground main object name
 
     if(collision.gameObject.name == "Ground"){
         
         if(!GroundSensor){
             
             GroundSensor = true;
         }
 
     }
     
 }
 function OnCollisionStay(collision : Collision) {
 
 // "Ground" is Ground main object name
 
     if(collision.gameObject.name == "Ground"){
         
         GroundSensor = true;
     }
     
 }
 function OnCollisionExit(collision : Collision) {
 
 // "Ground" is Ground main object name
 
     if(collision.gameObject.name == "Ground"){
         
         if(GroundSensor){
         
             GroundSensor = false;
         
         }
 
     }
     
 }
 
                 You could try doing something along these lines to make the planes speed be influenced by whether it is moving up or down
 if(!GroundSensor){
 
   //this will return either a positive or negative value
   var verticalSpeed = rigidbody.velocity.y;
  
   Debug.Log("Plane Vertical Speed: " && verticalSpeed);
 
   //add/subtract the vertical speed to the planes forward speed 
   rigidbody.velocity.z += verticalSpeed;
 
 }
 
                  Or you might have to replace this:
  rigidbody.velocity = transform.forward * Speed;
 
                  With this:
 var verticalSpeed = rigidbody.velocity.y;
 
 rigidbody.velocity = transform.forward * (Speed + verticalSpeed);
 
                 A lot of thanks dear friend for your help. But in the rotation to left or right yet have problem.
No problem, I converted the comment into an answer so you can mark it as accepted if it has answered your question :D
Answer by ByteSheep · Mar 10, 2013 at 12:42 PM
You could try changing the lines 68 to 74 to this:
 if(Input.GetKey(KeyCode.Q))
 {
   transform.Rotate(0,-Acceleration*Time.deltaTime,0);
 
   //reduce the speed slighlty when the player makes the plane rotate
   rigidbody.velocity.z -= Acceleration * Time.deltaTime;
 }
 
 if(Input.GetKey(KeyCode.E))
 {
   transform.Rotate(0,Acceleration*Time.deltaTime,0);
 
   //reduce the speed slighlty when the player makes the plane rotate
   rigidbody.velocity.z -= Acceleration * Time.deltaTime;
 }
 
   
 
               If this works then you might want to add a check to see if the forward speed is greater than a certain amount so the plane won't come to a full stop in mid-air ;)
Holy moly, what just happend?! $$anonymous$$y karma just went up 200? :o
;) Credit where credit is due (and on some answers long overdue)
Your answer