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 PersonMon · Nov 16, 2020 at 08:36 PM · rotation3dclampclamped rotation

How to use mathf.clamp?

My look script works fine, but you can look upside down.

 float v = Sens * Input.GetAxis("Mouse Y");
 v = Mathf.Clamp(v, -80, 80);
 CamQuar.Rotate(-v, 0, 0);
 float h = Sens * Input.GetAxis("Mouse X");
 CamQuar.RotateAround(PlayerQuar.position, Vector3.up, h);

it doesn't return any errors and runs fine in unity, but you can still turn all the way around. I have no idea why it wont do anything.

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

2 Replies

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

Answer by Spip5 · Nov 16, 2020 at 09:04 PM

The Mathf does work in your example, but you are missing the logic of what you have written here :). What you did here is to clamp the amount by which you rotate, not lock the rotation.

Transform.Rotate() is different than Transform.rotation or Transform.eulerAngles. Rotate() will rotate by the amount you give (even if Clamped, it keeks on rotating by the clamped values you gave it. Try to put it in the Update loop to see the result). In the other hand rotation and eulerAngles define the actual rotation of your object.

You need something that will look like this :

 float v = Sens * Input.GetAxis("Mouse Y");
 Transform.eulerAngles = new Vector3(mathf.Clamp(v,-80,80),0,0);

I havn't ried it out, but I am pretty convinced this should work you want you intend it to !

Comment
Add comment · Show 5 · 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 PersonMon · Nov 16, 2020 at 09:18 PM 0
Share

This, unfortunately does not work for me. It turns correctly for 1 frame before snapping back to 0, 0, 0 rotation.

avatar image Spip5 PersonMon · Nov 16, 2020 at 09:25 PM 0
Share

That's because I have no clue what you are looking for. If it is FPS-like, then it should look like this. Edit : missed a +=

  float v;
 
 Update()
 {
 if(Input.GetAxis("$$anonymous$$ouse Y") != 0) {
 {
     v += Sens * Input.GetAxis("$$anonymous$$ouse Y")
 }
  Transform.eulerAngles = new Vector3(mathf.Clamp(v,-80,80),0,0);
 }
 
avatar image PersonMon Spip5 · Nov 16, 2020 at 09:41 PM 0
Share

Yes it is in first person, and with the new script im having the same problem, but instead of snapping to 0 rotation, it smoothly transitions to it.

Show more comments
avatar image
0

Answer by myzzie · Nov 16, 2020 at 08:54 PM

You're applying rotation rather than setting it. Had you debugged the v value you'd seen that it's only ever what value Sens have multiplied by -1 or 1. So every frame you're applying a rotation of a fixed x value;

edit:

 public float sensitivity;
 private float x;
 private float y;
 x += sensitivity * Input.GetAxis("Mouse X");
 y += sensitivity * Input.GetAxis("Mouse Y");
 y = Mathf.Clamp(y, -80, 80);
 camera.transform.rotation = Quaternion.Euler(-y, x, 0);

I included the y axis but I'm guessing you rotate the body around y and only the camera around x axis.

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 PersonMon · Nov 16, 2020 at 09:04 PM 0
Share

So what should i do to fix it?

avatar image myzzie PersonMon · Nov 16, 2020 at 10:54 PM 0
Share

I edited a fix

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

231 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to clamp character rotation on Y and X axis? 1 Answer

3D top down rotation 1 Answer

How to Clamp a Quaternion Camera Rotation? 0 Answers

How to clamp rotation of an object? 2 Answers

Clamped rotation of turret problem 0 Answers


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