Keeping character trapped inside trigger area not working

Hi guys, I am trying to limit my character’s movement to a constrained area with a trigger, but it’s not quite working. It almost works, but My character controller breaks free at certain angles… Does anyone know how to fix this?
Source Code:
`
private Vector3 lastPosition;

void OnTriggerStay (Collider other)
{
	//This records the last position the object was fully INSIDE the trigger
	lastPosition = other.transform.position;
}

void OnTriggerExit (Collider other)
{
	//This resets the character's position to last positition fully inside the trigger if they try to exit
	other.GetComponent<Transform>().position = lastPosition;
}`

For some reason the code in my post is not formatting correctly. Here it is:

	private Vector3 lastPosition;

	void OnTriggerStay (Collider other)
	{
		//This records the last position the object was fully INSIDE the trigger
		lastPosition = other.transform.position;
	}

	void OnTriggerExit (Collider other)
	{
		//This resets the character's position to last positition fully inside the trigger if they try to exit
		other.GetComponent<Transform>().position = lastPosition;
	}

What if you were to limit the character’s movement. What exactly is set up in your scene. Is it 3D or 2D. Some more information would probably help you.