Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Hanneswall · Jun 02, 2010 at 06:41 PM · limitrotaion

Limiting rotation of an object

Hi,

I'm making a little game where you control a bunker on the ground defending itself from bombers and paratroopers. The bunker has a cannon which the player can rotate to aim at her enemies. My problem is that there is no limit to the rotation, so right now the player can aim the cannon straight down into the ground. What I would like to do is to limit the possible rotation between the values of, say, 0 to 180 along the x-axis.

So how can I limit the rotation values for an object?

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

6 Replies

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

Answer by Eric5h5 · Jun 02, 2010 at 06:49 PM

Take a look at the MouseLook script in Standard Assets for a way of limiting rotation.

Comment
Add comment · Show 5 · 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 qJake · Jun 02, 2010 at 06:51 PM 0
Share

@Eric This would only apply if the cannon was in first-person mode.

avatar image Eric5h5 · Jun 02, 2010 at 07:18 PM 0
Share

@SpikeX: No, the principle is the same. You wouldn't actually use the $$anonymous$$ouseLook script, you'd use the angle-limiting bits.

avatar image qJake · Jun 02, 2010 at 07:38 PM 0
Share

Oh. Yeah, that would work, but it would require a bit of script modification, and I tend to not put too much faith into the people asking the questions. ;)

avatar image Eric5h5 · Jun 02, 2010 at 08:26 PM 1
Share

I'd rather assume that people usually need enough detail to answer the question in most cases, and can ask follow-up questions if they need more info, rather than trying to cover every possible situation upfront. $$anonymous$$ouseLook is pretty straightforward and everybody has it, so I didn't think there was a reason to paste the code here in this case.

avatar image Hanneswall · Jun 08, 2010 at 07:05 PM 0
Share

Thanks a lot! It works, although I'm not exactly sure how. :P
Like SpikeX correctly assumes I don't have a lot of coding experience so all those quaternions and so on were a bit hard to wrap my head around.

Anyway, Thanks a lot! I wouldn't have figured it out without your help!

avatar image
22

Answer by joel_b · May 24, 2013 at 02:16 AM

You can use the same code from MouseLook.cs. The following code will lock rotation between -90 and 90 degrees in the Z-axis.

Here is an example:

 // gobal 
 private float rotationZ = 0f;
 private float sensitivityZ = 2f;
 
 void lockedRotation()
 {
    rotationZ += Input.GetAxis("Mouse X") * sensitivityZ;
    rotationZ = Mathf.Clamp (rotationZ, -90, 90);
             
     transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
 }
 
 
 
Comment
Add comment · Show 4 · 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 skylem · Dec 28, 2014 at 01:12 PM 0
Share

$$anonymous$$s answer provides the best Solution, using the axis to first affect the rotation is the best way to demonstrate the clamp, thanks.

avatar image animalphase · Apr 28, 2015 at 06:10 AM 0
Share

Hi @joel_b , I had a related issue and came across this post while searching. Just wanted to say thanks for taking the time to write this, I've had an issue with Quaternions in the $$anonymous$$ouseLook script for a long time, but never altered they way I handled rotations until reading your transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ); line.

After that I was able to make my force-player-look code work just fine without weird interactions with the Quaternion. So thanks for spurring on my thinking!

avatar image SoldierofYHVH · Aug 04, 2017 at 10:47 PM 0
Share

Short, sweet, and effective! Thanks!

avatar image XenoRo · May 07, 2019 at 06:24 PM 0
Share

There is a slight improvement that can be made to this. Ins$$anonymous$$d of separating rotationZ into it's own variable, and then repeating tranform.localEulerAngles._, you can use the fact that Vector_ are structs, and passed-by-value. Just manipulate the final localEulerAngles vector directly and then plug it back to the transform:

 var rotation = transform.localEulerAngles;
 rotation.z = $$anonymous$$athf.Clamp (rotation.z + Input.GetAxis("$$anonymous$$ouse X") * sensitivityZ, -90, 90);
 transform.localEulerAngles = rotation;

avatar image
2

Answer by pset · Jul 25, 2017 at 02:00 PM

You can use quaternion function Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) to restrict rotation like this:

 Quaternion ClampRotationXByLookDirection(Quaternion curentRotation)
 {
     Vector3 lookDirectionX = Vector3.ProjectOnPlane(lookDirection, Vector3.right).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(lookDirectionX);
     Quaternion towardsRotation = Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaX);
     return towardsRotation;
 }
 
 Quaternion ClampRotationYByLookDirection(Quaternion curentRotation)
 {
     Vector3 lookDirectionY = Vector3.ProjectOnPlane(lookDirection, Vector3.up).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(lookDirectionY);
     Quaternion towardsRotation = Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaY);
     return towardsRotation;
 }
 
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
avatar image
1

Answer by qJake · Jun 02, 2010 at 06:56 PM

You can limit the rotation of an object like this, for example if you wanted to limit the X-rotation of an object between 30 and 150 degrees:

float MinClamp = 30; float MaxClamp = 150;

// Other movement needs to be calculated first, so // we use LateUpdate() instead of Update(). void LateUpdate() { transform.rotation.eulerAngles = new Vector3( Mathf.Clamp(transform.rotation.eulerAngles.x, MinClamp, MaxClamp), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z ); }

You can obviously modify this to clamp any angle and any value you want, and it can be in its own script file, you don't have to include it in another script.

Comment
Add comment · Show 4 · 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 Eric5h5 · Jun 02, 2010 at 07:30 PM 0
Share

Limiting a single axis using eulerAngles tends to be quite problematic due to internal quaternion conversions. For example, this code will fail when you try to rotate X past 90. It won't work for values like $$anonymous$$=-30, max=30 either. It's generally better to track rotations yourself, like the $$anonymous$$ouseLook script does. (Just to nitpick, transform.rotation.eulerAngles is redundant; you only need transform.eulerAngles.)

avatar image qJake · Jun 02, 2010 at 07:38 PM 0
Share

Ah, didn't know that. Is there some better way of limiting like this (using Update) than using the euler angles?

avatar image Eric5h5 · Jun 02, 2010 at 08:19 PM 0
Share

@SpikeX: Yes, use your own variable for tracking the rotation in combination with a ClampAngle function, and apply that to the transform, like $$anonymous$$ouseLook does. :)

avatar image XenoRo · Aug 17, 2019 at 10:31 PM 1
Share

@Eric5h5 I think you can get the negatives to work with a little bit of logic. Like:

 var localEuler = transform.localEulerAngles;
 
 if (localEuler.z > 180)
     localEuler.z = -360 + localEuler.z;
 localEuler.z = $$anonymous$$athf.Clamp(localEuler.z, $$anonymous$$, max);
 
 transform.localEulerAngles = localEuler;

As a (way) more experienced programmer, are there any pitfalls you can see with something like that?

avatar image
1

Answer by blented · Oct 03, 2015 at 02:37 PM

You can use Quaternion.Slerp() to limit the rotation of an object, for instance:

 transform.localRotation = Quaternion.Slerp(rot, Quaternion.Euler(Vector3.forward), limit);

This would blend between Quaternion rot and forward based on limit.

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
  • 1
  • 2
  • ›

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

8 People are following this question.

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

Related Questions

How do I limit the x axis on a first person controller? 1 Answer

Acceloration speed limit 1 Answer

How to limit FPS on non-retina devices? 0 Answers

How do you limit velocity on a rigid body in only one direction? 1 Answer

Having a fuel limit 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