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
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                