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 alone1992 · Mar 10, 2013 at 11:58 AM · directionflightairplane

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

Comment
Add comment · Show 6
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 ByteSheep · Mar 10, 2013 at 12:03 PM 1
Share

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 :)

avatar image alone1992 · Mar 10, 2013 at 12:05 PM 0
Share
     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;
         
         }
 
     }
     
 }
avatar image ByteSheep · Mar 10, 2013 at 12:20 PM 2
Share

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);
avatar image alone1992 · Mar 10, 2013 at 12:37 PM 1
Share

A lot of thanks dear friend for your help. But in the rotation to left or right yet have problem.

avatar image ByteSheep · Mar 10, 2013 at 01:03 PM 1
Share

No problem, I converted the comment into an answer so you can mark it as accepted if it has answered your question :D

Show more comments

1 Reply

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

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 ;)

Comment
Add comment · Show 6 · 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 ByteSheep · Mar 10, 2013 at 01:05 PM 1
Share

Hah, I just reached 1k ;P

avatar image alone1992 · Mar 10, 2013 at 01:13 PM 1
Share

you should reach more than 1k. :)

avatar image AlucardJay · Mar 10, 2013 at 04:49 PM 1
Share

That is awesome =]

avatar image ByteSheep · Mar 10, 2013 at 04:56 PM 0
Share

Holy moly, what just happend?! $$anonymous$$y karma just went up 200? :o

avatar image AlucardJay · Mar 10, 2013 at 05:01 PM 0
Share

;) Credit where credit is due (and on some answers long overdue)

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Flight sim script help 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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