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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Difficulty with mouse movement (Solved)

Discussion in 'Scripting' started by maxrio, Mar 29, 2015.

  1. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Greetings. I'm having trouble when configuring the motion of a 2D object with the mouse. I need the object to be confined in the walls and that has only movement on the y axis when dragging with the mouse clicked. In the script below I am unable to confine the object and movements are on the x axis as well.
    I am not able to adjust the script. I appreciate the help.



    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var moveSpeed : float = 0.1;
    4. var mousePosition : Vector2;
    5. var springSpeed : SpringJoint2D;
    6. var velFrequency : float;
    7.  
    8.  
    9.      
    10. function Start()
    11. {
    12.  
    13. }  
    14.  
    15. function Update()
    16. {
    17.     if (Input.GetMouseButton(0))
    18.     {
    19.         springSpeed.frequency = 0;
    20.            mousePosition = Input.mousePosition;
    21.            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    22.            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    23.     }
    24.    
    25.     if(Input.GetMouseButtonUp(0))
    26.     {
    27.         springSpeed.frequency = velFrequency;
    28.     }
    29.  
    30. }
     

    Attached Files:

  2. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    Try this

    Code (JavaScript):
    1. transform.position = Vector2.Lerp(transform.position, Vector2(transform.position.x, mousePosition.y), moveSpeed);
     
    maxrio likes this.
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    This should lock the X-axis:
    Code (CSharp):
    1.         springSpeed.frequency = 0;
    2.            mousePosition = Input.mousePosition;
    3.            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    4.            mousePosition.x = transform.position;
    5.            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    6.  
     
    maxrio likes this.
  4. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    OK but:
    Code (JavaScript):
    1. mousePosition.x = transform.position.x;
     
  5. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Thank you.
     
  6. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    And how can I restrict the object inside the box? It crosses the box when moving to the extremities.
     
  7. jallen720

    jallen720

    Joined:
    Feb 22, 2015
    Posts:
    66
    This will give you a way to set the bounds for movement:

    Code (JavaScript):
    1. // These are the x & y bounds
    2. var xMin : float;
    3. var xMax : float;
    4. var yMin : float;
    5. var yMax : float;
    6.  
    7. // Clamp the new x-position based on transform.position.x between xMin and xMax
    8. var xPos : float = Mathf.Clamp(transform.position.x, xMin, xMax);
    9.  
    10. // Clamp the new y-position based on mousePosition.y between yMin and yMax
    11. var yPos : float = Mathf.Clamp(mousePosition.y, yMin, yMax);
    12.  
    13. // Lerp to new position that is clamped between bounds
    14. transform.position = Vector2.Lerp(transform.position, Vector2(xPos, yPos), moveSpeed);
     
    maxrio likes this.
  8. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Thank you. I will test and I report.
     
  9. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    :) That'S Perfect! Solved the problem. Thank you so much for your help. :)
     
    jallen720 likes this.