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
1
Question by Aladine · Oct 29, 2014 at 04:46 PM · 2drotationphysicsplane

2D plane physics, add torque based on direction

Hello,

Am trying to figure out how Retry game by Rovio works(watch this if you don't know the game : https://www.youtube.com/watch?v=z8hwVmLqAgg )

At first i thought it simply need to add positive torque when i press down and add negate torque when release, while making the plan move forward of course. unfortanately that gave me a very strange result, i kept trying and so far this is the best thing i came up with :

 using UnityEngine;
 using System.Collections;
 
 public class PlanPhysics : MonoBehaviour
 {
 
         Rigidbody2D body;
         Transform trans ;
         public float flyPower;
         private float flyPower_P;
         public float rotatePowerUp, rotatePowerDown;
         private float rotatePowerUp_P, rotatePowerDown_P;
         public float rotateUpLimit, rotateDownLimit;
         public float speed ;
         private float speed_P;
         public float maxSpeed;
         Vector3 euler;
         public float angular;
         bool pressed;
         
         // Use this for initialization
         void Start ()
         {
                 body = rigidbody2D;
                 trans = transform;
                 euler = trans.eulerAngles;
                 //
                 resetVariables ();    
         }
         
         void resetVariables ()
         {
                 rotatePowerUp_P = rotatePowerUp;
                 rotatePowerDown_P = rotatePowerDown;
                 flyPower_P = flyPower;
                 speed_P = speed;
         }
 
         void rotateDown ()
         {
 
                 euler = trans.eulerAngles;
                 if (euler.z < 290) {
                         if (angular > -rotateDownLimit) {
                                 rotatePowerDown_P += 200 * Time.deltaTime;
                                 angular -= rotatePowerDown_P * Time.deltaTime;
                         }
                         body.angularVelocity = angular;
                 } else {
                         angular = 0;
                         body.angularVelocity = angular;
                 }
         }
 
         void rotateUp ()
         {
                 euler = trans.eulerAngles;
         
                 if (angular < rotateUpLimit) {
                         if (angular < 0) {
                                 rotatePowerUp_P += 300 * Time.deltaTime;
                         } else {
                                 rotatePowerUp_P += 100 * Time.deltaTime;
                         }
                         angular += rotatePowerUp_P * Time.deltaTime;
                 }
                 body.angularVelocity = angular;
         }
 
         void flyUp ()
         {
                 body.AddForce (new Vector2 (0, flyPower_P));
                 if (flyPower_P > 0) {
                         flyPower_P -= 10 * Time.deltaTime;
                 } else {
                         flyPower_P = 0;
                 }
         }
 
         void moveForward ()
         {
                 speed_P += 100 * Time.deltaTime;
                 body.AddRelativeForce (Vector2.right * speed_P);
                 if (body.velocity.magnitude > maxSpeed) {
                         body.velocity = rigidbody2D.velocity.normalized * maxSpeed;
                 }
         }
 
         // Update is called once per frame
         void Update ()
         {
                 //    
                 if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton (0)) {
                         rotateUp ();
                         flyUp ();
                         moveForward ();
                 } else {
                         if (pressed) {
                                 rotateDown ();
                         }
                 }
 
 
                 if (Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp (0)) {
                         pressed = true;
                         resetVariables ();    
                 } 
                 
         }
 
         void OnCollisionEnter2D (Collision2D coll)
         {
                 angular = 0;
                 body.angularVelocity = angular;
                 pressed = false;
         }
 }
 

  

Now at the start it looks like it worked perfectly, but i found a "game kill" issue, say you kept pressing until the plane is leaning to the left vertically ( \ ) at certain point in a real plane if you leave everything and let physics do its job the plane should continue to rotate toward the left direction, but in my case it will go back to the right, this is expected because that's what i asked it to do by calling "rotateDown" when am not pressing anything, so what i need to know is how to figure out if i have to rotate the plane "down" with negative values (to the left) or with positive values (right).

I made this little .GIF to make everything clear : https://dl.dropboxusercontent.com/u/88553415/fastGif/PlanPhsx.gif
(when the sky is dark it means am not pressing down)

Thank you very much and have a nice day.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aladine · Oct 29, 2014 at 07:44 PM

Okay so i change the rotateDown() method to this and it gave me the result i needed however am still thinking that there is a better way to do it :

     void rotateDown ()
             {
     
                     euler = trans.eulerAngles;
                     if (euler.z > 120 && euler.z < 280) {
                             if (angular > -rotateDownLimit) {
                                     rotatePowerDown_P += 200 * Time.deltaTime;
                                     angular += rotatePowerDown_P * Time.deltaTime;
                             }
                             body.angularVelocity = angular;
                     } else if (euler.z > 280 && angular > 20) {
                             angular = 10;
                 
                     } else if (euler.z < 120) {
                             if (angular > -rotateDownLimit) {
                                     
                                     rotatePowerDown_P += 200 * Time.deltaTime;
                                     angular -= rotatePowerDown_P * Time.deltaTime;
                             }
                             body.angularVelocity = angular;
                     }
                     
             }

Comment
Add comment · Show 3 · 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 Madeck · Oct 29, 2014 at 08:28 PM 0
Share

Ins$$anonymous$$d of duplicating your angular rotation, you can probably simplify if to :

 void rotateDown ()
 {
    
    euler = trans.eulerAngles;
    if (euler.z > 280 && angular > 20) {
        angular = 10;
        
        } else if (euler.z < 280) {
            if (angular > -rotateDownLimit) {
                
                rotatePowerDown_P += 200 * Time.deltaTime;
                // the parenthesis below should return either -1 or 1.
                angular += (euler.z<120? -1:1) * rotatePowerDown_P * Time.deltaTime;
            }
            body.angularVelocity = angular;
        }
        
    }
avatar image Aladine · Oct 31, 2014 at 02:16 AM 0
Share

Thanks but you just made the code shorter, that wasn't what i meant by a "better solution", till now my plane physics still doesn't feel like i wanted to, i will observe the game more and try to see what i need. thanks

avatar image DiscoFever · Feb 20, 2015 at 10:14 PM 0
Share

Nice script. Have you got better results ?

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

26 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 avatar image avatar image 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

C# Water Rotation 0 Answers

Rotating an object to match a rigidbody's direction of movement. 2 Answers

Character still walking when no key is already pressed 1 Answer

How To Fix My RayCastHit2D in Unity 4.3.4? 0 Answers

Camera Scrolling just track the Charakter if z-Rotation=180 0 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