- Home /
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
Answer by Seth-Bergman · Jul 02, 2012 at 02:50 AM
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:
I keep getting an error. •Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
See link above.. So, you'll need to reassign the Vector3, rather than just the z coordinate, looks like.. Still maybe not the most elegant solution though
It looks like it should work, but now the character isn't appearing. Is there any way to make my character static in the z-direction?
Will be looking through posts.
check the other link above, it's got a bit more fleshed out example.. once you hit play on the scene, you can pause it and select the character in the hierarchy view to find it, may help figure out what's going wrong.
O$$anonymous$$, my character is continously falling... Luckly not in the z-direction. Your other link is great, but gives a very undesired effect of seeing the character jump back everytime it touches something slanted.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make GameObject move in one direction continuously on key press on C# 1 Answer
Basic AI Movement C# 3 Answers
How can I properly integrate rotation to a character? 0 Answers
How to stop object from rotating during movement (Javascript) 1 Answer
Constraining an object's movement in one direction by range 0 Answers