• 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 Mad_Mark · Jan 07, 2018 at 05:46 PM · rotation axis

Rotate Throttle "x" between 0 & 145, and back

I am working on a flight sim, and am trying to rotate the throttle gameobject to match the thrust. Pressing the spacebar adds thrust, pressing "b" reduces thrust. Full thrust is achieved at 145 on x axis. Zero thrust at 0 on x axis. I've run out of ideas. Any help would be appreciated.

 //        if (Input.GetKey(KeyCode.B))
 //        {
 //            throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, minThrottleRotation.x, Time.deltaTime), 0, 0);
 //            throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
 //        }
 //
 //        if (Input.GetKey(KeyCode.Space))
 //        {
 //            throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, maxThrottleRotation.x, Time.deltaTime), 0, 0);
 //            throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
 //        }
 
         if (Input.GetButton("Thrust") && Input.GetAxisRaw("Thrust") > 0) 
         {
             throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, maxThrottleRotation.x, Time.deltaTime), 0, 0);
         } 
         else if (Input.GetButton("Thrust") && Input.GetAxisRaw("Thrust") < 0) 
         {
             throttleModelCurrentRotation = new Vector3 (0f,0f,0f);//new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, -1f, Time.deltaTime), 0, 0);
         }
         throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
 

With this code, the throttle moves forward, but it doesn't move back to 0.

Mark

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 Diukrone · Jan 13, 2018 at 02:24 PM

Use AddForce to get propulsion. Using Lerp you are just doing a force move on direct transform values. All Vector3 and Quaternion should to be in Fixed Update. To simulate the force from your inputs, you can use this pseudo code as reference:

  public Rigidbody targetVehicle; //The object that we want to apply force to move and control it.
       //Using rigidbody we will can apply forces and change values on Vector3 and Quaternion properties.
     float stabilityAxis; //Stability Axis helps to Axis come back to zero value.
     float wantedAxis; //Wanted Axis are the axis that we want to reach.
     float currentAxis; //Current Axis are the axis that are right now on Axis Input.
     float smoothAxis; //Smooth the movement of our Axis Input.
     float velocityAxis; //The speed that will do the axis movement.

void FixedUpdate(){

  AxisControl();  
    targetVehicle.AddForce(Vector3.up * upForce * currentAxis);

//Here we apply force to our object using force instead of Lerp.

      targetVehicle.rotation = Quaternion.Euler(new Vector3(0, currentAxis, 0)); }   
      //Here we apply the Axis Input values on AddForce to create propulsion and on Quaternion to create direction movement. Look how current Axis are on parameters, change it to see diferente effects.

void AxisControl(); {

if(Input.GetAxis("Horizontal") <= 0) { wantedAxis += stabilityAxis; }

//This get the negative value from Axis input and store the value on Wanted Axis.

else if(Input.GetAxis("Horizontal") >= 0) { wantedAxis -= stabilityAxis;}

//This get the positive value from Axis input and store the value on Wanted Axis.

else if(Input.GetAxis("Horizontal") == 0) { wantedAxis == stabilityAxis; }

//If no Axis input is being applyed, zero the values in Wanted Axis.

wantedAxis = Mathf.Clamp(wantedAxis, -50, 50);

//Now we limit the value that Axis can reach on positive or negative sides. Use // on this line code to remove all move restriction.

currentAxis = Mathf.SmoothDamp(currentAxis, wantedAxis, ref velocityAxis, smoothAxis;}}

//Now we will change our current Axis transform position value while we change our Axis input values. The stored currentAxis values will be applyed now at our throttle code in Fixed Update section.
}

}

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

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

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

Related Questions

Rotate camera with mouse input depending on base rotation of player (3D) 2 Answers

How to control rocket pathing with Quaternion.Slerp 1 Answer

How come when I rotate my spaceship, it also rotates on the two other axis's? Please help? 0 Answers

How set rotation of a single axis 1 Answer

Quaternions rotation stuck 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