Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to use Mathf,Clamp, and more, to draw a menu from the side to a max point

Discussion in 'Scripting' started by pKallv, Aug 3, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am afraid i need to ask the community for help with this.



    I have a menu on the left that I want to be able drag, by the knob, into the scene only to a certain point, which is so the menu is fully seen. I then should be able to return it back.

    I have been trying all kind of things as well as searching, including Mathf.Clamp but with no real success. I am able to drag it out on the scene but when it passes the limit it blinks and when i drop it it jump back to were i want it to stop.

    I would like to kindly ask for some help to understand how to do this so I can get this to work as:

    1. Click-n-drag on the knob out on the scene
    2. It should not be able to move up or down
    3. It should stop at the point were the whole menu is visible
    4. I should then be able to move it back so only the knob is seen

    The code is in a mess and there is some "old" stuff from other testing still in there but here is the last, totally unsuccessful, version i have been testing:

    Code (csharp):
    1.  
    2. void OnDrag_sidebar1(DragGesture gesture) {
    3. //Drag sidebar out
    4.     if (gesture.Selection.tag == "sidebarknob1") {
    5.     //print ("===OnDrag_sidebar1=== X: " + xx + " / Y: " + yy);
    6.  
    7.         if (gesture.Phase == ContinuousGesturePhase.Started) {
    8.             if(gesture.Selection != null) {
    9.  
    10.                 position = Camera.main.ScreenToWorldPoint(gesture.Position); //x = -3.7, y = -4.2, z = -10.0
    11.                     print (position);
    12.  
    13.         }
    14.         else if (gesture.Phase == ContinuousGesturePhase.Updated) {
    15.              if(gesture.Selection != null) {
    16.                  transform.position = new Vector3(Mathf.Clamp(transform.position.x, -3.7f,-2.98f),transform.position.y, transform.position.z );
    17.         }
    18.  
    I use FingerGestures from http://fingergestures.fatalfrog.com

    BTW, the ugly graphics is just ugly graphics
     
    Last edited: Aug 3, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Looks about right to me, except, you aren't using the gesture position on line 16.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    thanks, still does not work.

    I decided to abandon the drag and implemented single-click and animated the move of the knob-menu with Vector3.Lerp.
     
    Last edited: Aug 3, 2014