Best/easiest way around this plane problem?

Apologies for the poorly described title. My brain is fried. The problem below in the picture… I want to move that capsule in any direction via a mouse click but not outside the circle. The circle is a texture on a plane attached to the capsule object. So I did this bit of code:

using UnityEngine;
using System.Collections;

public class UnitMove : MonoBehaviour {
	
	public Transform moveTo;
	public int moveSpeed;
	public int rotationSpeed;
	
	void Start () {
	
	}
	
	void Update () {
		if (Input.GetMouseButtonDown(1)) {
			GameObject[] sl;
			sl = GameObject.FindGameObjectsWithTag("White");
			foreach(GameObject slt in sl) {
				UnitStats us = (UnitStats)slt.GetComponent("UnitStats");
				if (us.selected == true && us.movementphase == 0) {
					RaycastHit hit;
					Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
					if (Physics.Raycast(ray, out hit) && hit.transform.tag == "selectedmovecircle"){
						moveTo = hit.transform;
						Debug.Log (moveTo);
        				moveUnit();
					}
      			}
			}
		}
	}
			
	void moveUnit() {
		
	}
}

Which I was very proud of. In moveUnit() I’ll move the unit to moveTo. Now obviously I didn’t think ahead and realise that Unity won’t see that circle as a circle! It just sees the square-shaped plane. So my little capsule can move outside the circle to the corner edges of the plane.

Any ideas on the best way to get around this? Or am I going to have to get into some trigonometry (gulp)? Is another way better anyway?

Thanks.

758-problem.jpg

Bonus Secondary Question! Can anyone give me a pointer which commands I should be looking at to not render the part of that circle texture that is hanging over the side of the grass?

I used a simmilar code to place buildings in a RTS game I was playing around with. They would spawn at the hit point of the Raycast but only if they were a certain distance from another building.

I’m sure you could adapt your code to use the Vector3.Distance function. Simply have a maxium distance that your player can move to you can base this on the diameter of your circle texture, use the terrain or the ground platform to check if the raycast is hitting it that way you won’t be able to fall off the ground, then check if the distance from the player to the raycast hit point is less than the maximum movement distance.

Could look something like this …

var dist = Vector3.Distance(transform.position, hit.point);
if(dist > 10)
{
   //call your moveUnit function to the hit.point, something like
   moveUnit(hit.point);


}