• 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 Oorcuss · May 25, 2015 at 09:47 PM · rotationeuleranglescontrols

Trouble applying rotation locally instead of globally

Hi all, could use a fresh set of eyes as I have been struggling with this script.

I am trying to have a ship control like an aircraft, where when it is rolled 90 degrees and the stick is pulled back the ship's nose will follow the horizon parallel instead of perpindicular towards the zenith. However, if I pull the stick back while in a roll my nose will go towards the sky not pull my jet into a turn.

I believe my problem in the script is the world rotation values are being changed instead of local but I cannot figure out where.

Thanks for any input you have.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     private Rigidbody m_Rigidbody;
     private Vector3 euler;
 
     public float speed = 5.0f;
     private float trueSpeed = 0.0f;
     public float turnSpeed = 5.0f;
     public float strafeSpeed = 5.0f;
     
     public Vector3 strafe;
     private float roll;
     private float pitch;
     private float yaw;
     
 
     public float sensitivity = 10.0f;
     public float yawSmooth = 0.5f;
     public float rollSmooth = 0.75f;
     public float pitchSmooth = 0.55f;
 
     public GameObject bullet1;
     public Transform bulletSpawn;
 
 
     void Start (){
         m_Rigidbody = GetComponent<Rigidbody>();
         
 
         euler = transform.localEulerAngles;
         
 
         roll = Input.GetAxis("Roll");
         pitch = Input.GetAxis("Pitch");
         yaw = Input.GetAxis("Yaw");
         
     }
 
     // Update is called once per frame
     void Update () {
         strafe = new Vector3(Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime, 
                          Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime,
                          0);
 
         var power = Input.GetAxis ("Power");
 
         //Truespeed Controls
         if(trueSpeed < 10 && trueSpeed > -3){
             trueSpeed += power;
         }
         if(trueSpeed > 10){
             trueSpeed = 9.99f;
         }
         if(trueSpeed < -3){
             trueSpeed = -2.99f;
         }
         if(Input.GetKey ("c")){
             trueSpeed = 0f;
         }
 
         if(Input.GetKey ("f")){
             m_Rigidbody.freezeRotation = true;
             m_Rigidbody.freezeRotation = false;
         }
 
         roll = Input.GetAxis("Roll");
         pitch = Input.GetAxis("Pitch");
         yaw = Input.GetAxis("Yaw");
 
 
         if(Input.GetMouseButtonDown(0)){    
             Debug.Log("Boom");
 
             var temp = Instantiate(bullet1, 
                                    bulletSpawn.transform.position, 
                                    bulletSpawn.transform.rotation);
         }
 
 }
 
 
     void FixedUpdate(){
         //apply ship forces
         GetComponent<Rigidbody>().AddRelativeForce(0.0f ,0.0f, trueSpeed*speed*Time.deltaTime);
     
 
         //////////////////////////////////////////////////////////////////////////////////////////
         
         
         // Yaw over time smoothed        
         euler.y += yaw * turnSpeed * Time.deltaTime / yawSmooth;
         
 
         // Roll over time smoothed
         euler.z += roll * turnSpeed *Time.deltaTime / rollSmooth;
         
     
         // Pitch over time smoothed
         euler.x += pitch * turnSpeed * Time.deltaTime / pitchSmooth;
 
         
         // Apply rotation and apply some smoothing
         Quaternion rot = Quaternion.Euler(euler);
         //transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
         transform.localRotation = Quaternion.Lerp (transform.localRotation, rot, sensitivity);
         //////////////////////////////////////////////////////////////////////////////////////////
         
         //apply strafing
         GetComponent<Rigidbody>().AddRelativeForce(strafe);
 
     }
 }
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 memorymod · May 25, 2015 at 11:56 PM

transform.localRotation?

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 Oorcuss · May 26, 2015 at 06:35 AM 0
Share

Look near the bottom of my script, this is the function I am calling to rotate. Hence my question as to why its rotating in the world system.

avatar image memorymod · May 26, 2015 at 04:29 PM 0
Share

Try transform.Rotate ins$$anonymous$$d of using your own function, I find that using Quaternion.Euler's can be a little buggy.

avatar image Oorcuss · May 26, 2015 at 05:57 PM 0
Share

Thanks for the response. How can i rotate locally if i am using transform.rotate? $$anonymous$$y controls won't allow me to use world rotation without serious work on modifying outputs based on ship position.$$anonymous$$ath i don't want to have to do unless needed. Sorry for any typos i am typing this on my phone.

avatar image memorymod · May 27, 2015 at 12:05 AM 1
Share

Transform.Rotate(0, 0, sensitivity * Time.deltaTime);

Or whatever axis you need, also sorry for any typos, I am on my phone also :)

avatar image Oorcuss · May 27, 2015 at 12:35 AM 0
Share

SIR!

While reading your comment at work I seriously doubted it would work, however in my busy state of mind that my job puts me in I missed the minute detail of the capital T in Transform!!

Just got home changed my code to do input based on this code.....

  private Transform tform;
  
  tform = GetComponent<Transform>();
  if(pitch != 0){
       tform.Rotate(pitch * pitchSmooth * Time.deltaTime,0,0);
  }

 

Thank you very much I knew it was something simple! Worked like a charm.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Compare euler angles to check if they describe the same rotation. 1 Answer

rotate an object by 180° on click only once 1 Answer

rotate a object around the x axis like other object... stucks at 90 degrees 2 Answers

Object deforms with rotation 1 Answer

How can I store the individual values of EulerAngles on a Database then re-apply 2 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