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

Updated 5 days ago in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 5 days ago

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)
		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