How to add movement limits to the character?

movement limit X, in C#

xPos = 0;
transform.position = new Vector3(xPos, someValue, someValue);

Where do i put “xPos = 0”?

You need to learn to code.
Do you know how to move a player?
Lets see your code.

My player move so:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]

public class pinguim : MonoBehaviour
{
public float speed = 20.0f;
public float rotateSpeed = 3.0f;

void Update ()
{
CharacterController controller = GetComponent();
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
transform.position += transform.right * speed * Time.deltaTime;
controller.SimpleMove(forward);

}
}

A few things.

  1. When you post code, in the forums, use
 tags. So it looks like this...
[code]someFunction();
  1. Update() is called every frame. That’s a lot of times per second. You used GetComponent() in Update. that is bad for the engine. you only need to get it once and cache it, so, instead, place it in Start() or Awake().
  2. You appear to be moving your object via two methods. Why? If you adjust transform.position, why use controller.simpleMove()? It makes no sense.

Now, if you want to limit its xPos, I would create an xLimit variable, and place it in the update.

if(transform.position.x > xLimit)
speed = 0;// this will halt any movement.

There are a million ways to limt the x movement. This is just one.

Ok, i miss too much when i do something because i’m beginner, it’s my first experience on unity.

So i’ve to add a float “xLimit”, assign a value to it and past this code in the update?

Something like that.
Just use a lot of google.
Example, “unity move transform”, “unity rotate object”… Etc etc.

A million ways to limit the x movement? Nice. So i need another, because this didn’t worked :smile: