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 Adam_Raetz · Oct 15, 2015 at 10:56 AM · c#mathfmathf.clampclamped rotation

Another Mathf.Clamp Not Working on Rotate?

I've been trying many of the post solutions to this problem and it's just not coming together.

My input should be from the keyboard WASD. I am trying to rotate an object with those keys. I have a custom pivot point for the object, which mostly works, but I really want to clamp the rotation so that it doesn't go all wonky on me.

Right now, my code below does allow the rotation, but there's no clamp at all. It's like it doesn't see it.

Any help would be awesome, please.

    public float speed = 5f;


 void update () {

        float moveh = Input.GetAxis("horizontal") * speed * Time.deltaTime;

        float movev = Input.GetAxis("vertical") * speed * Time.deltaTime;

        moveh = Mathf.Clamp(moveh, -5, 5);
        movev = Mathf.Clamp(movev, -50, 50);

        transform.Rotate(movev, moveh, 0);
        
         
        

 } 


Not sure what I am missing.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Munchy2007 · Oct 15, 2015 at 02:26 PM

You are clamping the amount of rotation you are adding, and not the actual rotation of the object.

This example will clamp the rotation 40 degrees either side of zero on the y axis. You should be able to modify it to suit your needs.

 public float speed = 20f;
 
     void Update () {
         float moveh = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         float movev = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         
         transform.Rotate(movev, moveh, 0);
 
         var rot = transform.rotation.eulerAngles;
         if(moveh > 0 && (rot.y > 40 && rot.y < 320)) rot.y = 40;
         if(moveh < 0 && (rot.y < 320 && rot.y > 40)) rot.y = 320;
 
         transform.rotation = Quaternion.Euler(rot);
 
     } 

This may not be the best or most efficient approach, but it does work and might help you work out a better solution.

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 Adam_Raetz · Oct 16, 2015 at 04:45 AM 0
Share

Thanks $$anonymous$$unchy, that really seemed like it would work and it was an approach I hadn't tried.

Alas, no cookie.

As an aside, whenever I use anything that called any of the Eulers it will immediately flip the game object (a weapon in this case) around to face the player the moment I try to rotate the item in game mode.

avatar image Adam_Raetz · Oct 16, 2015 at 09:56 AM 0
Share

Thanks $$anonymous$$unchy, that really looked like it would work and it was an approach I hadn't tried yet, but alas.... still no cookie.

As an aside, it seems like anything that calls any Euler seems to immediately flip the weapon which is being rotated. So it ends up pointing right back at the player.... it's kind of comical.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Not able to clamp camera rotation 1 Answer

Object rotation using Mathf.Atan2 0 Answers

Trigonometry Unit Circle Question 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