Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question 2d game, moving platform with mouse

Discussion in '2D' started by Essomba11, May 24, 2024.

  1. Essomba11

    Essomba11

    Joined:
    Jan 17, 2024
    Posts:
    3
    Hello, I'm doing a platformer for my class and I'm trying to make the platforms move with the mouse, I already have the platform moving on 2 differents axis but i can't get them to have a limit in the movement, I don't know if a clamp is usable for that or if I need to create Max height variable or anything else I've tried with children and register the position at the start but how can i define them as a limit ? Would appreciate some help thanks


    Code (CSharp):
    1. public class MoveObject : MonoBehaviour
    2. {
    3.     private List<Vector2> CheckPointsPositions = new List<Vector2>();
    4.  
    5.  
    6.     private bool dragging = false;
    7.     private Vector2 offset;
    8.  
    9.     [SerializeField] float heightMax;
    10.     [SerializeField] float heightMin;
    11.  
    12.     private Vector2 originalPos;
    13.  
    14.     [SerializeField] private bool isVertical;
    15.  
    16.     void Start()
    17.     {
    18.         for (int i = 0; i < transform.childCount; i++)
    19.         {
    20.             CheckPointsPositions.Add(transform.GetChild(i).position);
    21.         }
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         if (!dragging) return;
    27.  
    28.         var mousePosition = GetMousePos();
    29.         if (isVertical)
    30.         {
    31.             Vector2 newPos = new Vector2(transform.position.x, mousePosition.y - offset.y);
    32.             transform.position = newPos;
    33.  
    34.  
    35.         }
    36.         else
    37.         {
    38.             Vector2 newPos2 = new Vector2(mousePosition.x - offset.x, transform.position.y);
    39.             transform.position = newPos2;
    40.  
    41.         }
    42.  
    43.  
    44.     }
    45.  
    46.     private void OnMouseDown()
    47.     {
    48.         dragging = true;
    49.  
    50.         offset = GetMousePos() - (Vector2)transform.position;
    51.     }
    52.  
    53.  
    54.     private void OnMouseUp()
    55.     {
    56.         dragging = false;
    57.     }
    58.  
    59.     Vector2 GetMousePos()
    60.     {
    61.         return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    62.     }
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    293
    newPos.y = Mathf.Clamp(newPos.y, heightMin, heightMax)
    ?
     
    Essomba11 and dstears like this.
  3. Essomba11

    Essomba11

    Joined:
    Jan 17, 2024
    Posts:
    3
    Yeah that's what I did it with mousePosition.y a little bit later and it worked thank you