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 /
avatar image
0
Question by Davrun · Apr 28, 2017 at 06:03 AM · physicstransform.rotationmathf.clampmathf.lerpquaternion.slerp

Limiting rotation in 2 axis'

I have spent many many hours working on something unsuccessfully and am just shy of given up, I wrote out this question once already and my sleeping girlfriend decided to roll over my mouse and somehow managed to click something that took me back to the sites index, deleting all I had written...

Back on track now: Basically I have lost a whole nights sleep trying to stop my toy darn car in the game i'm making flipping upside down all the blooming time (please insert profanities where seen fit) and I have had 0 success, I've tried Mathf.Clamp and Lerp and Slerp and zeroing the rotation whenever the axis rotates over a certain point and now I just want to shoot my laptop.

Anyone who can shed some light on the conclusion on this will forever be my hero... please help...

[Here's my hours worth of work, as written and unwritten with the backspace]

 //logic workings set in scene
 public float speed;
 public Vector3 startPoint;

 //In game items sourced
 //private Rigidbody rb;

 void Start () {
     //gets the RigidBody to relate to the code
     //rb = GetComponent<Rigidbody>();
 }
     
 void Update () {
     //Movement control
     if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
     {
         transform.position -= transform.forward * Time.deltaTime * speed;
         if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
         {
             transform.Rotate(0, -2, 0);
         }

         if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
         {
             transform.Rotate(0, 2, 0);
         }
     }
     else
     {
         if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
         {
             transform.position += transform.forward * Time.deltaTime * speed;
         }

         if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
         {
             transform.Rotate(0, 2, 0);
         }

         if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
         {
             transform.Rotate(0, -2, 0);
         }
     }

     //rotation control
     if (transform.localRotation.x > 30 || transform.localRotation.x < -30)
     {
         transform.rotation = Quaternion.Euler(0, transform.rotation.y, transform.rotation.z);
     }
 }

Someone please save me from this hell thanks in advance - Dave

Comment
Add comment · Show 3
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 Awis · Apr 28, 2017 at 07:16 AM 0
Share

try this one to adjust in your code

void start() { var horiz = Input.GetAxis("Horizontal"); Vector3 goLeftRight = new Vector3(horiz, 0, 0);

     transform.Translate(goLeftRight);

}

avatar image Davrun Awis · Apr 28, 2017 at 01:54 PM 0
Share

Unfortunately the rotation in Y and Z axis' are controlled by the rigid body so I'd need code that limited that.

avatar image janus1001 · Apr 28, 2017 at 05:52 PM 0
Share

I would appreciate more info about your problem, it seems very chaotic. Is game 2D? Code doesn't seem to change any other axes. Did you try setting RigidBody "Constraints", which limit the rotation that is changed by the Physics? Also, I don't know what you are going for, but the code for rotation looks like it is very sturdy - have you considered using Forces and Torques that are in RigidBody?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by laurentlavigne · Apr 28, 2017 at 05:45 PM

I was going to skip that one because too much whining and girlfriend bashing but here it is, now buy her flowers toapologize, you douche.

alt text


rb-constraint.png (17.0 kB)
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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

moving object while it has another movement 1 Answer

gameobject still rotating over z axis 0 Answers

Mathf.Clamp acts strange when going below 0 1 Answer

2D 360 degress platformer example needed 0 Answers

Tracking a projectile with Mathf.Clamp 1 Answer


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