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 /
  • Help Room /
avatar image
0
Question by graiderzs · Nov 25, 2015 at 08:53 PM · c#rotation2d gamelimitmouse-drag

2D How to set a limit to the rotation of my cube, when dragging with mouse

Hello everyone,

I have a 2D game. I am trying to make a character selection page, and player is able to select by dragging the mouse to the left or right.

What I did was, I have a 3D cube and 3 characters. each character is on each side, left middle and right.

1st character - left (y-axis, 270)

2nd character - middle (y-axis, 0)

3rd character - right (y-axis, 90)

I start off with the y-axis, 0, meaning I am currently facing the 2nd character.

When I drag the mouse to the left, the 3D cube will rotate towards the right on the y-axis, which would lead me to the 1st character.

Rotate towards right - (subtract y-axis)

Rotate towards left - (add y-axis)

I am able to rotate it, however, now i would like to set a rotation limit in which, when i drag the mouse to the left from the 1st character, player is not able to rotate it anymore when it reaches 215 on y-axis and from there, if player drag mouse towards right, the 3D cube will rotate towards left (back to 1st character).

I am aware that there is a Mathf.Clamp, however, I do not know how to use it properly in this context.

In summary,

-Rotation limit, when 3D cube y-axis rotation reaches 215, cannot rotate, until player rotate the other way.

-Rotation limit, when 3D cube y-axis rotation reaches 135, cannot rotate, until player rotate the other way.

Here is the code,

 public class selectionmanagerScript : MonoBehaviour {
 
     public Vector2 startMouse;
     public Vector2 currentMouse;
     public Vector2 lastMouse;
 
     public GameObject selection_panel;
     public Vector2 selection_panal_startPos;
 
     public float rotationY;
     public bool rLeft;
     public bool rRight;
     public bool rMiddle;
     public float fixSpeed = 2f;
 
     public float fixedValue;
 
     public bool dragLeft;
     public bool dragRight;
 
     // Use this for initialization
     void Start () {
 
         selection_panal_startPos = transform.position;
     }
     
     // Update is called once per frame
     void Update () {
 
         mouseSwipe();
         fixToObject();
     }
 
     public void mouseSwipe()
     {
         rotationY = selection_panel.transform.eulerAngles.y;
 
         if (rotationY > 180.0f)
         {
             rotationY -= 360.0f;
         }
         
         if (Input.GetButtonDown("Fire1"))
         {
             startMouse = Input.mousePosition;
 
             fixedValue = selection_panel.transform.eulerAngles.y;
 
             rLeft = false;
             rRight = false;
             rMiddle = false;
         }
 
         if (Input.GetButton("Fire1"))
         {
             //calculate how much did the player drag
 
             currentMouse = Input.mousePosition;
 
             //move the selection panel based on how much did the player drag
             if (Input.GetAxis("Mouse X") != 0)
             {
                 if (Input.GetAxis("Mouse X") > 0)
                 {
                     dragRight = true;
                     dragLeft = false;
                 }
                 if (Input.GetAxis("Mouse X") < 0)
                 {
                     dragRight = false;
                     dragLeft = true;
                 }
 
                 selection_panel.transform.eulerAngles = new Vector3(0, fixedValue + (currentMouse.x - startMouse.x) / 5, 0);

             }
         }
     }
 
     //fixed to object
     public void fixToObject()
     {
         //check which object is the player near to
         if (Input.GetButtonUp("Fire1"))
         {
             //player is near to left object
             if (rotationY < -45 && rotationY > -180)
             {
                 rLeft = true;
                 rRight = false;
                 rMiddle = false;
             }
             //player is near to right object
             if (rotationY > 45 && rotationY < 180)
             {
                 rLeft = false;
                 rRight = true;
                 rMiddle = false;
             }
             //player is near to middle object
             if (rotationY >= -45 && rotationY <= 45)
             {
                 rLeft = false;
                 rRight = false;
                 rMiddle = true;
             }
         }
         //fixed to left object
         if (rLeft)
         {
             selection_panel.transform.rotation = Quaternion.Euler(0, Mathf.Lerp(selection_panel.transform.eulerAngles.y, 270, Time.deltaTime * fixSpeed), 0);
         }
         //fixed to middle object
         if (rRight)
         {
             selection_panel.transform.rotation = Quaternion.Euler(0, Mathf.Lerp(rotationY, 90, Time.deltaTime * fixSpeed), 0);
         }
         //fixed to right object
         if (rMiddle)
         {
             selection_panel.transform.rotation = Quaternion.Euler(0, Mathf.Lerp(rotationY, 0, Time.deltaTime * fixSpeed), 0);
         }
     }
 }


Code Explanation:

-Selection panel is the 3D cube

-rLeft, rMiddle, rRight, is if player stops dragging, the 3D cube will rotate to the nearest sides, either these 3, (y-axis), 270, 0, 90.

rLeft - 270

rMiddle - 0

rRight - 90

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

0 Replies

· Add your reply
  • Sort: 

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

37 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

Related Questions

Rotate GameObject with children around itself on mouse drag 1 Answer

How do I limit an object to rotate back and forth between 2 angles? 1 Answer

How would I orient VTOL engines relative to the direction of a ship's movement? 1 Answer

Rotate Sphere? 1 Answer

[VR] Make a bow follow the 'holding object' AND look at the 'string pulling hand' 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