Search Unity

Need help stopping the player from leaving the play area with touch controls

Discussion in '2D' started by mrasif, Dec 3, 2019.

  1. mrasif


    Nov 24, 2019
    Here is my move script:

    public class MoveDifferent : MonoBehaviour { private Vector3 mousePosition; public float moveSpeed = 0.1f; public bool isGrounded = false;

    // Use this for initialization
    void Start () {


    // Update is called once per frame
    void Update () {
    if (Input.GetMouseButton(0) && isGrounded) {
    mousePosition = Input.mousePosition;
    mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);



    Here is a an image of what I am trying to do:

    I want the player to be able to touch around the green area where it will float too but if the player touches outside the play area (green area) it will go as far as it can towards that point until it hits the white space.

    Any ideas? I have tried making the green part a Boolean that will trigger as true when the player is on it and false when the player leaves but I realized how dumb that was since the player can leave and never re-enter when really I just want them to not be able to leave.