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
0
Question by DeadKenny · Feb 10, 2015 at 05:14 PM · rotationtransformquaternionlookatclamp

How to clamp the rotation of this?

This is part of a code for a turret with a barrel. It works almost perfect but the thing I am missing is the clamping of the barrels rotation up and down. There is a sort of clamp as can be seen by the if statement that checks weather the turret is facing a certain angle so the barrel only starts moving when facing within a certain area of where you are looking but that clamps all angles equally. Making it smaller clamps the vertical same as horizontal.

So what I need really is another clamp for rotations of the barrel itself. How can I do that?

Code:

 //Aimtarget is a raycast hit from the camera, the barrel follows it so long as the aimAngle is smaller than 50.
 
 //What I need to clamp is the barrel.rotation on x z and y. I tried all kinds of things like clamping the bRot.x = 0.0f but that just screws everything up. 
     
 
 
         aimTDir = transform.InverseTransformPoint(aimTarget.position);
 
     aimBAngle = Vector3.Angle(aimTDir, -transform.forward);
 
         if(aimBAngle < 50.0f){            
          Quaternion bRot = Quaternion.LookRotation(aimHitPoint - barrel.position);
 
          barrel.rotation = Quaternion.Slerp(barrel.rotation, bRot, Time.deltaTime * 5.0f);
             }
 
 // yeah, please help me clamp this barrel. Thanks.


PS: Quaternions really get me dumbstruck, so sorry for noobyness.

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 meat5000 ♦ · Feb 10, 2015 at 06:07 PM 0
Share

Uae $$anonymous$$athf.Clamp

avatar image troien · Feb 10, 2015 at 07:46 PM 0
Share

When I read this line:

 //What I need to clamp is the barrel.rotation on x z and y. I tried all kinds of things like clamping the bRot.x = 0.0f but that just screws everything up.

I think your problem might be that you are trying to clamp the wrong value. Quaternions x,y,z (and w) values do not represent the values you see in the inspector. The docs explain them as:

They are based on complex numbers and are not easy to understand intuitively

And the x,y,z and w have this comment next to them ;)

Don't modify this directly unless you know quaternions inside out.

Ins$$anonymous$$d you should get the eulerAngles, clamp it, and then convert it back to a Quaternion using Quaternion.Euler.

Something like this:

 Quaternion unclampedRotation; // set this to your value
 
 Vector3 euler = unclampedRotation.eulerAngles;
 
 euler.x = $$anonymous$$athf.Clamp(euler.x,$$anonymous$$Value,maxValue);
 
 Quaternion clampedRotation = Quaternion.Euler(euler);

(I wrote this without a editor, so it might contain silly syntax erors, but it should give you an idea)

If this was indeed the problem, please reply so that I can write this up as an answer ins$$anonymous$$d of a comment)

avatar image DeadKenny · Feb 11, 2015 at 12:05 AM 0
Share

Well I think my problem is mainly co$$anonymous$$g from the way I have everything set up. I have to use another method for the barrel.

Nothing is working so far.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers

Rotation - Simple Question 0 Answers

How to make an enemy look at you and follow you 0 Answers

Look at like rotating around y axis 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