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 /
This question was closed Jul 25, 2017 at 04:26 PM by mcdemrer for the following reason:

I reworked my code to try to get around this.

avatar image
0
Question by mcdemrer · Jul 24, 2017 at 06:14 PM · rotationvector3vectorclampclamped rotation

Clamping Vector3

I'm trying to clamp a Vector3 in Unity3D C#, and it the code isn't doing anything. Any tips?

Code:

 Vector3 rot = new Vector3(-Input.GetAxis("MouseY"), Input.GetAxis("MouseX"), 0);
 rot.y = Mathf.Clamp(rot.y, -clampAngle, clampAngle);
 
 transform.Rotate(rot * Time.deltaTime * mouseSensitivity);
Comment
Add comment · Show 4
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 TreyH · Jul 24, 2017 at 06:37 PM 0
Share

Can you paste the definitions of your axes?

avatar image mcdemrer TreyH · Jul 24, 2017 at 07:11 PM 0
Share

Sorry, can you explain? I'm really new to this.

avatar image TreyH · Jul 25, 2017 at 12:54 PM 0
Share

Wait you're just trying to clamp the transform's rotation? Right now, you're clamping the rotation the transform will experience, but not how the transform will appear after that rotation.

You want something closer to:

 // $$anonymous$$ouse rotation
 Vector3 rot = new Vector3 (-Input.GetAxis ("$$anonymous$$ouseY"), Input.GetAxis ("$$anonymous$$ouseX"), 0);
 
 // Assign the rotation
 this.transform.Rotate (rot);
 
 // Get whatever its new rotation is
 Vector3 resulting = this.transform.rotation.eulerAngles;
 
 // Clamp that y-component
 resulting.y = $$anonymous$$athf.Clamp (resulting.y, -clampAngle, clampAngle);
 
 // Then reassign the clamped rotation
 this.transform.rotation = Quaternion.Euler (resulting);

avatar image NoseKills · Jul 25, 2017 at 03:15 PM 0
Share

Yeah it always helps to be specific about what you are trying to do: limit the end rotation of the object or the rotation speed.

2 Replies

  • Sort: 
avatar image
0

Answer by MattG54321 · Jul 24, 2017 at 07:15 PM

Try putting in Debug.Log and seeing what it says, like this:

 Vector3 rot = new Vector3(-Input.GetAxis("MouseY"), Input.GetAxis("MouseX"), 0);
 rot.y = Mathf.Clamp(rot.y, -clampAngle, clampAngle);
 Debug.Log(rot.y);
 transform.Rotate(rot * Time.deltaTime * mouseSensitivity);
Comment
Add comment · Show 3 · 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 mcdemrer · Jul 24, 2017 at 07:35 PM 0
Share

It just comes back with a number from about -4 to 4 with a couple outliers getting nearer to -12 and 12.

avatar image MattG54321 mcdemrer · Jul 24, 2017 at 07:37 PM 0
Share

What is clampAngle set to? Is Input.GetAxis("$$anonymous$$ouseX") working properly?

avatar image mcdemrer MattG54321 · Jul 24, 2017 at 07:50 PM 0
Share

All of the movement works properly, and clampAngle is set to 80.

avatar image
0

Answer by MattG54321 · Jul 25, 2017 at 12:45 PM

My hunch right now is that Input.GetAxis("MouseX") is simply never returning a value greater than 80 or less than -80 because the mouse is never moving fast enough. Try clamping it after you've multiplied it by mouseSensitivity, like this:

 Vector3 rot = new Vector3(-Input.GetAxis("MouseY"), Input.GetAxis("MouseX"), 0);
 rot = rot * mouseSensitivity;
 rot.y = Mathf.Clamp(rot.y, -clampAngle, clampAngle);
 transform.Rotate(rot * Time.deltaTime);
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

Follow this Question

Answers Answers and Comments

98 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

Related Questions

Clamp gyro rotation 0 Answers

Limit Rotation of physics object in 2D 1 Answer

Camera Clamping Third Person? 0 Answers

Clamping rotation of an object 0 Answers

Clamping or testing an angle between objects. 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