• 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 Henrique Dantas · Jan 23, 2011 at 09:08 PM · rotationmathf.clamp

Limiting Rotation

Well i'm making a spaceship racing game and i'm having some problems. I want to limit the rotation of the space ship so the player can't go to the inverse direction of the track. This is the script:

function Update(){

var horizRotate = Input.GetAxis("Horizontal") velocidadeDeRotacao Time.deltaTime;

transform.Rotate(Vector3(0,horizRotate,0));

Mathf.Clamp(transform.rotation.y, minY, maxY);

}

basically i want to limit the y rotation to around 45 and -45 degrees. i'm using Mathf.Clamp, but it doesn't worked for me. Is there an other way to do this? Thank you.

Comment
Add comment · Show 1
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 drudiverse · Apr 06, 2014 at 04:51 AM 0
Share

You can use Vector3.Angle as well to monitor deviation from wished direction.

         var deviation =  Vector3.Angle(transform.forward, Vector3.forward);// difference between craft and forwards vectors
 
         
  if ( deviation <45  ){
 var lookDirection = look.transform.position - transform.position;
 etc

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mike 3 · Jan 23, 2011 at 09:24 PM

If it were me, i'd just store the wanted rotation as a variable, and create the rotation each frame with clamped values:

var horizRotate : float = 0.0;

function Update() { horizRotate += Input.GetAxis("Horizontal") velocidadeDeRotacao Time.deltaTime; horizRotate = Mathf.Clamp(horizRotate, -45, 45); transform.rotation = Quaternion.Euler(0, horizRotate, 0); }

Small edit:

Don't modify transform.rotation.x/y/z/w - Those are quaternion values, not euler, so they're not in degrees, and the maths relating to them are kinda crazy

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

2 People are following this question.

avatar image avatar image

Related Questions

After adding script for rotation to look up down, look left right stopped working,After writing code for the rotation for looking up down, looking right left stopped 0 Answers

Can rotation be clamped when X produces -30 for both negative and positive? 1 Answer

Tilting A Hover Car, need to lock the rotation 1 Answer

Clamping Child's Rotation Relative To Partent's 0 Answers

This is probably a really simple problem that I can't figure out 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