ThirdPersonController question part II?

Hi everyone, got another question on the script ThirdPersonController.js? I added the following code to it:

function UpdateSmoothedMovementDirection ()
{
	var cameraTransform = Camera.main.transform;
	var grounded = IsGrounded();
	
	// Forward vector relative to the camera along the x-z plane	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;

	// Right vector relative to the camera
	// Always orthogonal to the forward vector
	var right = Vector3(forward.z, 0, -forward.x);

	//var v = Input.GetAxisRaw("Vertical");
	var h = Input.GetAxisRaw("Horizontal");

	// Are we moving backwards or looking backwards
	//if (v < -0.2)
	//	movingBack = true;
	//else
	//	movingBack = false;
	
	// restrict x-axis movement to specified values
	if ( transform.position.x > 4.0 )
		Debug.Log ( "X-axis value + 4.0 EXCEEDED!" );
			
	else if ( transform.position.x < -2.0 )
		Debug.Log ( "X-axis value - 2.0 EXCEEDED!" );
		
	
	var wasMoving = isMoving;
	//isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
	isMoving = Mathf.Abs (h) > 0.1;
		
	// Target direction relative to the camera
	//var targetDirection = h * right + v * forward;
	var targetDirection = h * right;
	
	// Grounded controls
	if (grounded)
	{
		// Lock camera for short period when transitioning moving  standing still
		lockCameraTimer += Time.deltaTime;
		if (isMoving != wasMoving)
			lockCameraTimer = 0.0;

		// We store speed and direction seperately,
		// so that when the character stands still we still have a valid forward direction
		// moveDirection is always normalized, and we only update it if there is user input.
		if (targetDirection != Vector3.zero)
		{
			// If we are really slow, just snap to the target direction
			if (moveSpeed < walkSpeed * 0.9  grounded)
			{
				moveDirection = targetDirection.normalized;
			}
			// Otherwise smoothly turn towards it
			else
			{
				moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
				
				moveDirection = moveDirection.normalized;
			}
		}
		
		// Smooth the speed based on the current target direction
		var curSmooth = speedSmoothing * Time.deltaTime;
		
		// Choose target speed
		//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
		var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
	
		// Pick speed modifier
		//if (Input.GetButton ("Fire3"))
		//{
		//	targetSpeed *= runSpeed;
		//}
		//else if (Time.time - trotAfterSeconds > walkTimeStart)
		//{
		//	targetSpeed *= trotSpeed;
		//}
		//else
		//{
		//	targetSpeed *= walkSpeed;
		//}
		
		moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
		
		// Reset walk time start when we slow down
		if (moveSpeed < walkSpeed * 0.3)
			walkTimeStart = Time.time;
	}
	// In air controls
	else
	{
		// Lock camera while in air
		if (jumping)
			lockCameraTimer = 0.0;

		if (isMoving)
			inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
	}
}

The code is from the 3D tutorial. I added the Debug.Log code to see if the engine can detect when Lerpz moves past a certain X axis value and it does it very well. Find the added code below. How would you folks go about restricting movement though when Lerpz reaches the max value? I know I could use planes to limit the movement but they look ugly and I’m trying to learn scripting anyways. So what would you folks do? I’m looking for ideas on how to tackle this issue. Thanks again.

if ( transform.position.x > 4.0 )
		Debug.Log ( "X-axis value + 4.0 EXCEEDED!" );
			
	else if ( transform.position.x < -2.0 )
		Debug.Log ( "X-axis value - 2.0 EXCEEDED!" );

Forcing values to stay within a certain range is known as “clamping”, and there is a Mathf.Clamp function to do just that.

Hi Andeeee, thanks for that quick tip. I added the following code and it works flawlessly. Thanks again.

var MinValueX = -2.0;
var MaxValueX = 4.0;

transform.position.x = Mathf.Clamp ( transform.position.x, MinValueX, MaxValueX );