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
1
Question by ExpiredIndexCard · Mar 24, 2013 at 11:30 PM · rotationmousemathfclamp

Quick Question About Limiting a Rotation.

Hey guys! I'm not the best person when it comes to rotations, sorry. I have a problem. I have a flashlight attached to my first person controller. This flashlight is not static, it follows the mouse. I want to limit the rotation of the flashlight so that it doesn't go too far off the screen. Here is the code to make the flashlight follow the mouse:

 Vector3 v3Pos = Input.mousePosition;
                v3Pos.z = moveArea;
               v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
                transform.LookAt(v3Pos);

And here is the code for my flashlight script:

 Mathf.Clamp(transform.rotation.x, -20f , 20f);
 Mathf.Clamp(transform.rotation.y, -30f , 30f);

The code to limit the rotation does not work. Please fix it and thanks a lot! :)

Comment
Add comment · Show 5
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 ExpiredIndexCard · Mar 25, 2013 at 12:33 AM 1
Share

Can anyone help? It will be greatly appreciated :)

avatar image cdrandin · Mar 25, 2013 at 12:44 AM 2
Share

Try using Euler angels. WHen you do transform.rotation.x, you are getting the rotation of x in quaternions, which is between -1 and 1 not sure on the values but essentially it is small

avatar image ExpiredIndexCard · Mar 25, 2013 at 01:38 AM 1
Share

Can you show me an example? I have never used Euler angles before

avatar image youngapprentice · Mar 25, 2013 at 01:41 AM 2
Share

http://docs.unity3d.com/Documentation/ScriptReference/Transform-eulerAngles.html

avatar image youngapprentice · Mar 25, 2013 at 01:42 AM 2
Share

Ooh. I really like the new design for the script reference. $$anonymous$$uch cleaner.

1 Reply

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

Answer by robertbu · Mar 25, 2013 at 02:15 AM

"transform.rotation" as you used above is a Quaternion. A Quaternion is a 4D construct, and generally you don't address the individual properties such as x,y,z and w. From the script reference:

[Quaternions] are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w);

You can get euler angles from a rotation, and as a shorthand, directly from the Transform. For example, you can read:

 Vector3 v3Pos = transform.euler;  

But are are issues with euler angles as well. First, for any physical rotation there are multiple euler rotations. So an object was spun around on the x axis by 180 degrees, you would expect a euler angle of (180,0,0), but the euler angle read back from the Transform might be instead (0,180,180) which represents the same rotation.

Second, you should not set individual components of a euler angle. You need to set all the as the same time by assigning a Vector3. From the script reference:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once.

So limiting rotation is not a simple thing. There are complex scripts written to normalize and clamp rotations, but there are also other ways of looking at some problems that make a solution easy.

One idea for your situation you might only set the position if the mouse is within a certain area of the screen. The following code (untested) limits the LookAt to a radius of 1/2 of the screen height. You can adjust the radius by adjusting the calculation for "max_pixel_radius".

 using UnityEngine;
 using System.Collections;
 
 public class Bug16 : MonoBehaviour {
     
     float max_pixel_radius = Screen.height / 4.0f;  
     Vector2 v2Center = new Vector2(Screen.width / 2.0f, Screen.height / 2.0f);
 
     void Update () {
         float fT = ((Vector2)Input.mousePosition - v2Center).magnitude;
         if (fT <= max_pixel_radius) {
             Vector3 v3Pos = Input.mousePosition;
             v3Pos.z = moveArea;
             v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
             transform.LookAt(v3Pos);
         }
     }
 }


Comment
Add comment · Show 1 · 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 ExpiredIndexCard · Mar 25, 2013 at 02:25 AM 1
Share

Very interesting! Works well but needs a few adjustments. I really like the way you handled that piece of code. I think it's very well executed and I will give you best answer. Thanks

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

12 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

Related Questions

gesture rotation clamp 2 Answers

I need help with clamping a rotation towards the mouse position 2 Answers

player flicks when mouse is near the player 1 Answer

Clamp Rotation Problem 1 Answer

How to limit the rotation of an object?? 2 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