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
public class MoveObject : MonoBehaviour
{
private List<Vector2> CheckPointsPositions = new List<Vector2>();
private bool dragging = false;
private Vector2 offset;
[SerializeField] float heightMax;
[SerializeField] float heightMin;
private Vector2 originalPos;
[SerializeField] private bool isVertical;
void Start()
{
for (int i = 0; i < transform.childCount; i++)
{
CheckPointsPositions.Add(transform.GetChild(i).position);
}
}
void Update()
{
if (!dragging) return;
var mousePosition = GetMousePos();
if (isVertical)
{
Vector2 newPos = new Vector2(transform.position.x, mousePosition.y - offset.y);
transform.position = newPos;
}
else
{
Vector2 newPos2 = new Vector2(mousePosition.x - offset.x, transform.position.y);
transform.position = newPos2;
}
}
private void OnMouseDown()
{
dragging = true;
offset = GetMousePos() - (Vector2)transform.position;
}
private void OnMouseUp()
{
dragging = false;
}
Vector2 GetMousePos()
{
return Camera.main.ScreenToWorldPoint(Input.mousePosition);
}