CharacterController - hard code X-axis movement to limit movement?

Hi, I’m trying to use the documented script for CharacterController to control a GameObject. Can I hard code the X-axis movement to limit the range that the player can walk/jump along the same X-axis? I would like to limit the player to plus or negative 10 transform.position.x. I tried adding a Mathf.Clamp to the player.transform.position.x and it won’t allow the counter in Inspector to go beyond +10 or -10 on the X-axis but the player keeps moving beyond the limit though in the scene.

Thanks in advance for any help.

Here’s my source code in c#:

// PlayerMovement.cs - main script that controls player

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private float posX;

// Update is called once per frame
void Update ()
{
	// player movement code here
	CharacterController controller = GetComponent<CharacterController>();
	posX = player.transform.position.x;
	if (controller.isGrounded)
	{
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		Debug.Log(posX);
		
		// check for jump here
		if (Input.GetButton("Jump"))
			moveDirection.y = jumpSpeed;
	}
	moveDirection.y -= gravity * Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
}

}

You need to zero out the application of “velocity” to moveDirection before it is applied to the transform in controller.Move

Move is a relative positioning method, not absolute which is why you’re seeing drift beyond your bounds with clamp. You won’t want to allow any movement on the X in this chunk of code if your aim is to bound the player.

You can always calculate the necessary offsets inside of this block before the force is applied however this may be glitchy visually.

Hi Tingham, thanks for the quick answer. I took your idea and ran with it to lower the speed value to 0.0F if the player position.x value is over 10.0F or smaller than -10.0F. The code below works perfect now as it stops the player dead. There’s a new bug now that all velocity stops and the player is frozen. I really appreciate the help and hopefully it will help others here. I owe you a beer!

public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float posX;
public float zeroedOutSpeed = 0.0F;
private Vector3 moveDirection = Vector3.zero;

// Update is called once per frame
void Update ()
{
	// player movement code here
	CharacterController controller = GetComponent<CharacterController>();
	posX = player.transform.position.x;
	if (controller.isGrounded)
	{
		// speed value code here
		if (posX > 10.0F || posX < -10.0F)
		{
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= zeroedOutSpeed;
		}
		
		else
		{
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
		}
		
		// check for jump here
		if (Input.GetButton("Jump"))
			moveDirection.y = jumpSpeed;
	}
	moveDirection.y -= gravity * Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
}

}