• 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
Question by D3mon1zA · Apr 27, 2016 at 03:20 AM · c#rotationcontrollerquaternioneuler

How to set a max rotation angle

Hey guys. I'm fairly new to scripting so I apollogise if this turns out to be really basic. I'm building a car controller rig. I have got a basic move script attached to the prefab as a whole and it works fine but I am wanting to set up some underlying scripts to control the wheels so they turn with the car to make it feel more realistic. I have the rotating down good but I cannot figure out how to set a maximum rotation so the wheels don't just keep rotating around into the wheel well. After spending days trawling the net for a sokution to my problem but cannot find a relevant answer. I have tried learning as much as I can about quaternions and euler angles but so far nothing I jave learned aboit them has helped. If anyone could give me some examples would be great and if you could explain why and how that way would work would be even better.

Here is a brief example of my rotating wheel code:

If (Input.GetKey (KeyCode.D)) { transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }

And this just basically repeats for each direction witha GetKeyUp function to return the wheels back to being adjacent with the car ounce it is done turning

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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by TheSorm · Apr 27, 2016 at 10:38 AM

Sonthofen like that shult work:

   If (Input.GetKey (KeyCode.D)) {
     If (Transformation.eulerangel() < yourangel){
      transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }
     }

(Don't know if eulerangel is the right function name)

Comment

People who like this

0 Show 7 · 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 D3mon1zA · Apr 29, 2016 at 05:45 AM 0
Share
         if (Input.GetKey (KeyCode.D)){
             if (transform.eulerAngles() < maxangle ); {
                 transform.Rotate (Vector3.up * turnspeed * Time.deltaTime);
             }
         }


I have tried this way as well as several other different ways of phrasing it and just keep getting this error message ; error CS1955: The member `UnityEngine.Transform.eulerAngles' cannot be used as method or delegate

avatar image TheSorm · Apr 29, 2016 at 07:35 AM 0
Share

Soory i ride that on my phone small mistake its just eulerAngles without "()"

    If (Input.GetKey (KeyCode.D)) {
      If (transform.eulerAngles < yourangel){
       transform.Rotate (Vector3.up turnspeed Time.deltaTime) ; }
      }
avatar image D3mon1zA · Apr 29, 2016 at 08:43 AM 0
Share

Algood. I thought the () seemed odd. Am still Getting Compiler errors though This Is my full Wheel Script. I think I am not setting the eulerAngles float right? I don't really know how to do this properly.

 using UnityEngine;
 using System.Collections;
 
 public class WheelsTurnScript : MonoBehaviour {
       public float turnspeed = 50f;
     public float ReturnNormal = 100f;
     public float maxrotation = 30f; //To Set Max Rotation
 
     
     void Update () {
 
         if(Input.GetKey (KeyCode.D)){
             if (transform.eulerAngles < maxrotation){
 //Compiler Error = Operator '<' Cannot Be Applied to Operands Of Type 'UnityEngine.Vector3' And 'UnityEngine.Transform'
                     transform.Rotate (Vector3.up * turnspeed * Time.deltaTime);
                 }
             }
         if (Input.GetKey (KeyCode.A)) {
             if (transform.eulerAngles < maxrotation){
 //Compiler Error = Operator '<' Cannot Be Applied to Operands Of Type 'UnityEngine.Vector3' And 'UnityEngine.Transform'
                 transform.Rotate (Vector3.up * -turnspeed * Time.deltaTime);
             }
         }
 
 //This Part To Return Wheels To Normal After Turning
 
         if (Input.GetKeyUp (KeyCode.A)) {
                     transform.Rotate (Vector3.up * ReturnNormal * Time.deltaTime);
                 }
         if (Input.GetKeyUp (KeyCode.D)) {
             transform.Rotate (Vector3.up * -ReturnNormal * Time.deltaTime);
         }
     }
 }
avatar image TheSorm · Apr 29, 2016 at 10:04 AM 0
Share

transform.EulerAngel is a Vector3 you have to say with rotation you need like "transform.eulerAngel.x"

avatar image D3mon1zA TheSorm · May 02, 2016 at 04:28 AM 0
Share

Is Almost working now Cheers. All Compiler errors are now gone, the only problem is when the object rotate's it doesn't stop completely and max rotation angle I have set, it just pauses for about a second and then continues to rotate until it gets to the next rotation point and then it does the same. for example it will go 45° and then pause, then it will go another 45° and then pause and so on and so fourth. What do you think Could be causing this and how should I go about fixing/changing it. Here is the kinda "Core" part of this script :

         void Update () 
             {
             if(Input.GetKey (KeyCode.D)){
                 if (transform.eulerAngles.y > maxrotation) {
                             transform.Rotate (Vector3.up *     turnspeed * Time.deltaTime);
                     }
                 }
             if (Input.GetKey (KeyCode.A)) {
                 if (transform.eulerAngles.y > maxrotation){
                             transform.Rotate (Vector3.up * -turnspeed * Time.deltaTime);
                     }
                 }

Thanks for your input so far has been really helpful

avatar image TheSorm D3mon1zA · May 02, 2016 at 09:58 AM 0
Share

Ehe do you use if(transform.eulerAngles.y > maxrotation){ And not if(transform.eulerAngles.y < maxrotation){

And to fix the problem with the steps just look in your inspector and look how the rotation change and what can cause this problem

avatar image TheSorm · Apr 29, 2016 at 10:06 AM 0
Share

And Euler angel rotation is from 0° to 360°

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

58 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

Related Questions

Child Object Rotation interpolation problem, 0 Answers

How can I set rotation properly? 0 Answers

-= Time.deltaTime 1 Answer

Adding one Objects Rotation To Another Objects Rotation 1 Answer

Why Quaternion.Euler can't rotate 360 Degrees 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