• 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 falconstrike209 · Dec 05, 2021 at 11:06 PM · rotationinputlock

How would I lock rotation in unity's new input sytem?

I am rotating an object towards the mouse or controller using unity's new input system, using this script.

 public class HandController : MonoBehaviour
 {
 
     public void ReadMouseInput(InputAction.CallbackContext context)
     {
         Vector2 mousePosition = context.ReadValue<Vector2>();
         Vector2 objectPosition = (Vector2)Camera.main.WorldToScreenPoint(transform.position);
         Vector2 direction = (mousePosition - objectPosition).normalized;
 
         RotateAim(direction);
 
     }
 
     public void ReadStickInput(InputAction.CallbackContext context)
     {
         Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
 
         RotateAim(stickDirection);
     }
 
     public void RotateAim(Vector2 direction)
     {
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
     }
 }

The rotation works well, however, I would like to be able to lock the rotation by setting a range between two values in which the object can rotate. I have been trying to find ways to do this, but pretty much any way of locking rotation I've found uses the old input system in some way. Is there a way I could implement this into my script using new input system? and if so, how would I go about doing that? thanks.

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 falconstrike209 · Dec 06, 2021 at 04:44 PM 0
Share

please help... anyone??

1 Reply

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

Answer by AaronBacon · Dec 06, 2021 at 05:00 PM

All you need to do is use Mathf.Clamp as you're setting the rotation

For example:

⠀

      public void RotateAim(Vector2 direction)
      {
          float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Clamp(angle,-75,75)));
      }

⠀

This should lock your rotation between -75 and 75 degrees for example

Comment
Add comment · Show 7 · 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 falconstrike209 · Dec 06, 2021 at 05:10 PM 0
Share

hm...

Assets\Scripts\HandController.cs(29,71): error CS1501: No overload for method 'Clamp' takes 2 arguments

avatar image falconstrike209 · Dec 06, 2021 at 05:11 PM 0
Share
 public class HandController : MonoBehaviour
 {
 
     public void ReadMouseInput(InputAction.CallbackContext context)
     {
         Vector2 mousePosition = context.ReadValue<Vector2>();
         Vector2 objectPosition = (Vector2)Camera.main.WorldToScreenPoint(transform.position);
         Vector2 direction = (mousePosition - objectPosition).normalized;
 
         RotateAim(direction);
 
     }
 
     public void ReadStickInput(InputAction.CallbackContext context)
     {
         Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
 
         RotateAim(stickDirection);
     }
 
     public void RotateAim(Vector2 direction)
     {
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Clamp(-75, 75)));
     }
 }

this is the script currently

avatar image falconstrike209 falconstrike209 · Dec 06, 2021 at 05:12 PM 0
Share

this is for a 2d game btw

avatar image AaronBacon falconstrike209 · Dec 06, 2021 at 05:20 PM 0
Share

Sorry, that's my fault, Mathf.Clamp(-75,75) should be Mathf.Clamp(angle,75,-75)

I've corrected the above code now, should work

avatar image falconstrike209 · Dec 06, 2021 at 05:23 PM 0
Share

it's working! although, now I have another error. The object being rotated is a pair of hands that is a child to the player, with a gun as a child to the hands. The error I am having is that now that the rotation is locked, the gun isn't flipping with the player properly. do you know how I could fix this? I am flipping the player using:

     public void Flip()
     {
         if ((inputX < 0 && facingRight) || (inputX > 0 && !facingRight))
         {
             facingRight = !facingRight;
             transform.Rotate(new Vector3(0, 180, 0));
         }
     }
avatar image AaronBacon falconstrike209 · Dec 06, 2021 at 05:30 PM 0
Share

Ah, so I think the problem is in the HandController script the same line that locks the rotation also sets its y rotation to 0. Try modifying that RotateAim function like so

⠀

      public void RotateAim(Vector2 direction)
      {
          float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, transform.position.y, Mathf.Clamp(-75, 75)));
      }


⠀

That should make it leave the other rotation values unchanged

avatar image falconstrike209 AaronBacon · Dec 06, 2021 at 05:46 PM 0
Share

nope, even with that changed it's still being wonk. If I leave my hand off of the things I use to aim, it will flip with the character. But as soon as I touch the right knob of a controller or the mouse, it snaps back to being flipped the other way

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

201 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 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 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 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 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 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 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 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 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 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

Need help on the 3ds max style camera control 0 Answers

Locking Rotation object on Z axis 1 Answer

Locking transform, rotation, scale, etc in the inspector window 1 Answer

Gyro calibration in android? 0 Answers

Lock rotation of an instantiated object to another 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