Constrained Movement for 2.5D character

I am a beginner at best, and I was wondering if there was a simple way of contraining the movement of my capsule character. I got the basic movement script off of the reference and made sure the constraints were on in the rigid body for movement in the z-axis, but when ever the character hit an object that is cuvered, it would be forced out of the my ideal plane, with positioning in the z-axis of 0.

Any ideas?

void Update() {
	
    CharacterController controller = GetComponent<CharacterController>();
    // is the controller on the ground?
    if (controller.isGrounded) {
        //Feed moveDirection with input.
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
        moveDirection = transform.TransformDirection(moveDirection);
        //Multiply it by speed.
        moveDirection *= speed;
        //Jumping
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;

    }
    //Applying gravity to the controller
    moveDirection.y -= gravity * Time.deltaTime;
    //Making the character move
    controller.Move(moveDirection * Time.deltaTime);
}

I have tried using this

transform.position.z = 0;

but I keep getting an error.

  • Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

It would help to post the script. the line “transform.position.z = 0;” in the Update function might work off the bat.

edit:

sorry didn’t know you’re using c#…
here’s a link that may help…

http://forum.unity3d.com/threads/66768-Set-value-on-GameObject.transform.position.x-with-C

So maybe try:

var x = transform.position.x;
var y = transform.position.y;

transform.position = new Vector3(x,y,0);

here’s another link that should be just what you want:

http://forum.unity3d.com/threads/91358-Restrict-Z-movement-on-character-controller-without-breaking-into-colliders