• 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
Question by Kennyist · Oct 10, 2013 at 07:33 AM · c#rotationtransformrotate objectrelative

Reletive rotation problems. (C#)

I'm trying to simulate a helicopter pitch/yaw/roll but I've run into a problem i cant find a solution for:

Demo:

http://tristanjc.com/unity/newweb.html

When you get into the air, the pitch and roll work fine from start rotation, but if you rotate yourself to face the camera (Z and C) and press pitch forward it tilts to the left and right instead. Aswell as other rotation bugs when rotating left or right.

The code for these bits are:

 // ===== Yaw ======= //
     
     if(Input.GetKey(KeyCode.Z)){
         if(engineReady){
             heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y + 0.01f, heli.transform.rotation.z, heli.transform.rotation.w);
         }
     }
     
     if(Input.GetKey(KeyCode.C)){
         if(engineReady){
             heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y - 0.01f, heli.transform.rotation.z, heli.transform.rotation.w);
         }
     }

  • Roll

           float temproll = roll / 1.66666f;
             temproll = (temproll - 30.0f) / 100.0f;
             rollDegrees = temproll;
             helirollSpeed = Mathf.Abs((temproll * 100.0f));
             
             heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y, rollDegrees, heli.transform.rotation.w);
    
    
  • pitch

           float tempPitch = pitch / 1.66666f;
             tempPitch = (tempPitch - 30.0f) / 100.0f;
             pitchDegrees = tempPitch;
             heliSpeed = Mathf.Abs((tempPitch * 100.0f));
             
             heli.transform.rotation = new Quaternion(pitchDegrees, heli.transform.rotation.y, heli.transform.rotation.z, heli.transform.rotation.w);
    
    
    

How would I keep rotation locked for each one.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Tomer-Barkan · Oct 10, 2013 at 08:13 AM

I would recommend using Transform.Rotate:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html

So you could have each control rotating around one specific axis (axis is relative to the direction your transform is facing), and shouldn't affect the others.

For example, if you'd like to roll, then you'd rotate around Vector3.forward:

 transform.Rotate(Vector3.forward * anglesPerSecond * Time.deltaTime);

(for rolling left you'd use -Vector3.forward instead)

Comment
Kennyist

People who like this

1 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 Kennyist · Oct 10, 2013 at 09:17 AM 0
Share

Thanks this worked fine, but how would i return the value for pitch if its on .forward, Would it still just be "heli.transform.rotation.x".

avatar image Tomer-Barkan · Oct 10, 2013 at 10:25 AM 0
Share

pitch would be rotating around the X axis, so you'd use transform.Rotate(Vector3.right * anglesPerSecond * Time.deltaTime);

avatar image Tomer-Barkan · Oct 10, 2013 at 10:26 AM 0
Share

and yaw would be rotating around the Y axis, so you'd use transform.Rotate(Vector3.up * anglesPerSecond * Time.deltaTime);

avatar image

Answer by $$anonymous$$ · Oct 10, 2013 at 08:38 AM

Using roll/pitch/yaw terminology from here: http://en.wikipedia.org/wiki/File:Rollpitchyawplain.png

You want to roll left/right around heli.transform.forward

You want to pitch up/down around heli.transform.right

You want to yaw left/right around heli.transform.up

Try using angles and Transform.RotateAround and see if that helps.

You can also try Quaternion.AngleAxis for each rotation then multiply the quaternions before setting heli.transform.rotation.

Comment
Kennyist

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Objects not rotating right half the time based on another object 0 Answers

Flip over an object (smooth transition) 3 Answers

transform.Rotate will not work 1 Answer

RotateAround Limitations 2 Answers

I need an object to rotate using a moving object as a pivot. 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