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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Object resizing, storing variable and resizing from previous size

Discussion in '2D' started by fivegrandpint, Jan 30, 2020.

  1. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    Hi

    So i have written a script to resize an object by clicking on it and dragging. The problem I'm having is that when I stop dragging and click on the object again, it snaps back to its initial size. I understand that what I have to do is store the size which it was previously resized to, and "start from this size" but I am struggling to get my head round the logic for this.


    Code (CSharp):
    1. public class DragScalingScript : MonoBehaviour
    2. {
    3.     public bool dragging = false;
    4.     public Vector3 currentMousePosition;
    5.     public Vector3 dragStartingPoint;
    6.     public Vector3 temp;
    7.     public float amountDragged;
    8.     private float clampedAmountDragged;
    9.  
    10.  
    11.     private void Awake()
    12.     {
    13.         amountDragged = 100.0f;
    14.  
    15.     }
    16.  
    17.  
    18.     // if object is clicked, dragging becomes true
    19.     // where ever the object is clicked becomes dragStartingPoint
    20.     private void OnMouseDown()
    21.     {
    22.  
    23.  
    24.         dragStartingPoint = (Input.mousePosition);
    25.  
    26.     }
    27.  
    28.  
    29.     //where ever the position of the mouse drag goes is tracked and stored as currentMousePosition
    30.     private void OnMouseDrag()
    31.     {
    32.  
    33.         dragging = true;
    34.         currentMousePosition = (Input.mousePosition);
    35.      
    36.  
    37.     }
    38.  
    39.     // when mouse button is released, dragging becomes false
    40.     private void OnMouseUp()
    41.     {
    42.         dragging = false;
    43.  
    44.         //finalAmountDragged = amountDragged;
    45.     }
    46.  
    47.     // when dragging:
    48.     private void Update()
    49.  
    50.     {
    51.         if (dragging == true)
    52.         {
    53.  
    54.             //find the distance between the dragStartingPoint and currentMousePosition and store in amountDragged variable
    55.             amountDragged = Vector3.Distance(currentMousePosition, dragStartingPoint);
    56.  
    57.          
    58.             // set minimum and maximum drag amount and store in clampedAmountDragged variable
    59.  
    60.             clampedAmountDragged = Mathf.Clamp(amountDragged, 100f, 300f);
    61.  
    62.             // set amountDragged to clampedAmount to apply minimum and maximum
    63.  
    64.             amountDragged = clampedAmountDragged;
    65.  
    66.                 temp = transform.localScale;
    67.                 temp.x = amountDragged / 100;
    68.                 temp.y = amountDragged / 100;
    69.  
    70.          
    71.             //make scale of object equal to temp variable
    72.             transform.localScale = temp;
    73.  
    74.        
    75.  
    76.         }
    77.  
    78.  
    79.  
    80.      
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87.     }
     
  2. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    I think I have to add a bool "hasBeenDragged" which will be made true if the amountDragged variable != 0 onMouseUp... and say in the update function, if hasBeenDragged = true && dragging = true - then this is the bit I'm struggling with. I need it to calculate the amountDragged based on the previous amount too but I cant get my head round how to write that.. Any help with the logic would be appreciated.

    Thanks in advance.