Unity2d: limit the travel distance of my player to half the size of the cameras width

Updated on May 16, 2018 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on May 16, 2018

I’m having troubles trying to limit the distance of my player from leaving a certain area, I want to shorten the distance to half the width of my main camera, is there a way to do so, this is my code:

void Update () {
       if (Input.GetMouseButtonDown (0)) {
	 if (EventSystem.current.currentSelectedGameObject)
		return;
		Vector3 mousePosition = Input.mousePosition - target;
		mousePosition.z = 10;
		target = Camera.main.ScreenToWorldPoint (mousePosition);
		target.y = transform.position.y;
					

		} 
	transform.position = Vector3.MoveTowards (transform.position, target, moveSpeed * Time.deltaTime);
    }

 

 

  • Liked by
Reply