Answer the question
In order to leave comments, you need to log in
How to stop UI panel from moving when it reaches screen border in Unity?
There is a panel that moves when you click on a certain area of the screen.
We need to make it so that the panel stops moving after reaching the edge of the camera / screen.
Source:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpgradeMenuScript : MonoBehaviour
{
public RectTransform upgradeMenu;
private Rect leftPart = new Rect(0, 0, Screen.width / 3, Screen.height);
private Rect rightPart = new Rect(Screen.width / 3 * 2, 0, Screen.width / 3, Screen.height);
private Rect upperPart = new Rect(0, Screen.height / 3 * 2, Screen.width, Screen.height / 3);
private Rect downPart = new Rect(0, 0, Screen.width, Screen.height / 3);
void Update()
{
if (Input.GetMouseButton(0))
{
if (leftPart.Contains(Input.mousePosition))
{
upgradeMenu.transform.Translate(new Vector2(15, 0) * Time.deltaTime);
}
if (rightPart.Contains(Input.mousePosition))
{
upgradeMenu.transform.Translate(new Vector2(-15, 0) * Time.deltaTime);
}
if (upperPart.Contains(Input.mousePosition))
{
upgradeMenu.transform.Translate(new Vector2(0, -15) * Time.deltaTime);
}
if (downPart.Contains(Input.mousePosition))
{
upgradeMenu.transform.Translate(new Vector2(0, 15) * Time.deltaTime);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question