Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by orchard800 · Aug 31, 2015 at 01:11 AM · rotationquaternionrotate objecttorqueaddtorque

Cant add Torque and Rotation to a rigidbody in the same frame?

Hi guys, could really do with some expert help here as I'm tearing my hair out with this! I cant find any other help articles which address this problem, so hopefully I'm overlooking something very simple.

Basically, I'm writing a top-down helicopter TPS (think Desert Strike). So far its going really well and I have a great model which flies around using proper physics and feels very real.

In each FixedUpdate I'm getting the 3 input axes (pitch, yaw, roll (the axes around x y z respectively)) and then calling my moveplayer method. This applies forces along x and z (y is constrained in the rigidbody settings) and applies a torque around y. So far it works beautifully.

Here's where the problem starts: I want to tilt the helicopter in the direction of travel, but when I apply any sort of rotation command to x or z to pitch or roll (x or z) it works but the torque command stops working so the player can't rotate left or right. Where am I going wrong?

Here's the gist of my code:

     void FixedUpdate ()
     {
             // Get pitch input (-1 to 1)
             float pitch = Input.GetAxis ("Vertical");
 
             // Get yaw (turn) input (-1 to 1)
             float yaw = Input.GetAxis ("Horizontal");
 
             // Get roll input (-1 to 1) - Keys Q & A
             float roll = Input.GetAxis ("Roll");
 
             // Move player
             MovePlayer (pitch, yaw, roll); 
     }
 
 
     void MovePlayer (float pitch, float yaw, float roll)
     {
         // move helicopter in pitch direction
         playerRigidbody.AddForce (transform.forward * pitch * thrust);    
 
         // turn helicopter in yaw direction
         playerRigidbody.AddTorque(transform.up * yaw * 100);
 
         // move helicopter in roll direction
         playerRigidbody.AddForce (transform.right * roll * thrust);
 
         //Clamp helicopter speed
         playerRigidbody.velocity = Vector3.ClampMagnitude (playerRigidbody.velocity, maxSpeed);
 
         // tilt helicopter in pitch direction - enabling this line stops rotation working!
         transform.Rotate (transform.right, pitch*20);
 }

I would be VERY grateful if anyone can help me. I've certainly had a lot of help from reading existing answers so far.

Cheers, Dave

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

2 Replies

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

Answer by orchard800 · Sep 20, 2015 at 03:49 AM

OK, I've sussed it myself! Here's the solution for anyone interested. (There's certainly nothing out there about this).

     // tilt helicopter 
     pitchRot = pitchInput * 10f; // pitchInput is vertical input axis
     rollRot = rollInput * 10f;  // rollInput is horizontal input axis
     transform.localEulerAngles = new Vector3(Mathf.Clamp (pitchRot, -20, 20), transform.localEulerAngles.y, Mathf.Clamp (-rollRot, -20, 20));

This allows addtorque to be applied around the Y axis.

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

Answer by hexagonius · Sep 19, 2015 at 09:36 PM

add all forces and torques together and apply them only once. they get overridden this way.

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 orchard800 · Sep 19, 2015 at 10:20 PM 0
Share

Ah great! Thanks for the advice. I have merged the 2 AddForce expressions together as suggested. It works just the same but it is neater and probably more efficient. I still cant rotate or use rotation or localrotation or eulerAngles (and so on) whilst toque is being applied though.

To clarify, I can tilt the helicopter back and forth using:

 // pitch helicopter -20 to 20 degrees using
 transform.localRotation = Quaternion.AngleAxis(pitchInput*20, Vector3.right);
 // OR
 transform.Rotate (transform.right, pitch*20);

but cannot add pitch AND roll (around Z) without freezing Y rotation (so torque stops being applied.)

Any ideas?

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

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

Related Questions

Help with suns rotation 0 Answers

Keep Rigidbody upright using torque. 1 Answer

Ask some problem about rotation, quaternion and angle 0 Answers

Rotating 3d person character 0 Answers

HoverBike rotation return Y axis to 0 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