• 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 DeadKenny · Nov 29, 2013 at 06:16 PM · vector3lookatweaponaimlookrotation

Clamping LookRotation.

I can't figure out how to clamp the rotation of my gun whilst it looks at the direction of the mouse.

Its just a gun on the hand of a character that rotates to the direction of a ray which is pointing to mouse position.

I want to clamp the maximum amount it rotates on the x and y in front of the player by about 180 degrees max.

I tried everything lol, can't do it. Please help.

 // ray aim direction.
 Vector3 aimDir = aimray.direction;
 
 
 gun.rotation = Quaternion.LookRotation(aimDir);
 
 










 
Comment
Add comment · Show 2
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 Julien-Lynge · Nov 29, 2013 at 06:23 PM 0
Share

I don't understand - the camera viewport is typically about 60 degrees, so if your gun is pointing where the mouse is, how can it turn more than 180 degrees?

avatar image DeadKenny · Nov 29, 2013 at 06:26 PM 0
Share

Oh crap I forgot to mention its a 3rd person game. I need it to stop going through the player when I'm looking around ai$$anonymous$$g from the hip. Only turn 180 max.

1 Reply

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

Answer by Julien-Lynge · Nov 29, 2013 at 06:33 PM

Just FYI: there isn't a 'right' way to do this. It's all math, and there are many ways to 'clamp' a value within a range, so this is just one method that would work.

  • Get the quaternion representing the player's orientation.

  • Then get the quaternion representing where the gun should be pointing (using LookRotation).

  • Call http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Angle.html to get the angle between 'character forward' and 'gun target'.

  • If this is greater than 180 degrees, use Slerp to clamp your angle. Pass to Slerp '180 / [angle]' as the last parameter, which is the fraction between the character's forward rotation and the target rotation where 180 degrees falls.

The result of this Slerp will be the rotation you want.

Comment
Add comment · Show 2 · 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 DeadKenny · Nov 30, 2013 at 09:42 AM 0
Share

Ok I think I almost have it, last part with passing to slerp confused me.

It looks around and there are no errors at leas, but doesn't clamp So in code:

 playerRotation = player.rotaion;
 
 Quaternion gunRotation = Quaternion.LookRotation(ray.direction);
 
 float gunRotAngle = Quaternion.Angle(playerRot, gunRotation);
 
 if(gunRotAngle > 180){
 
 
    gun.rotation = Quaternion.Slerp(playerRotation, gunRotation, 1.0f);
 
 // A bit lost at using the slerp to clamp the angle. 
 
 }
avatar image DeadKenny · Nov 30, 2013 at 12:17 PM 0
Share

Ok got it:

 // What I really wanted was a 90 degree clamp not a 180.
 
 
 if(gunRotAngle > 90)
 
 
 
 It works thanks. 
 
 

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

18 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

Related Questions

[HELP] Translating vector 2 co-ordinates to real space 1 Answer

Why LookRotation works, but LookAt doesnt 0 Answers

Rotate player to LookAt a touch position 2 Answers

Rotate an object so its up vector matches a sphere 1 Answer

Rotation via iTween.LookTo not working (or using it wrong) 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