- Home /
Make the Character not to move on the Z Axis [Locking Z Axis]
This may be the most banal Problem I ever had and it still doesnt leave me alone cuz I just cant fix it^^.
My Character Moves sometimes on the Z axis - its a sidescroller, so he shouldnt- sometimes when he collides with a stone or such, he slides arround it and... its really terrible. I really tried 1000 ways like Colliders on the side or with rigidbode-freeze Position or even with a simple script
function Update () { transform.position.z = 0; }
but somehow everytime the character is still able to move on the Z Axis.
Specially with the Script I thought it worked but then after some time, magicly, he fell right through a plane like nothing even happend^^ It really anoyes me because it eats my Time and Im not able to work efficient till this problem is fixed, so please community help me out Thxx =)
The Script that I use to Controll my Player http://jsfiddle.net/wx4gS/
You have to provide a lot more information about how you're moving the character to get meaningful feedback.
Sry - this is the script that i attached to the CharController http://jsfiddle.net/wx4gS/
Answer by timo0060 · May 14, 2012 at 07:25 PM
I had that same problem. My player would slowly move itself around the object. I solved it by doing this:
var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
moveDirection can be replaced with your variable. The second line of code is pretty much saying that moveDirection can move on the x axis, not the y, or the z however. I hoped this helped you.
wow thx Im definitivly trying it out soon thx =) do I have to add this to the current script or is a seperate one appropriet
Just put this in the same script. So just replace where you set the values of your movement variable with my code, and just change the variable moveDirection from my code to movement so it matches your code. That should work. And just change you key bindings in your game so that Horizontal is equal to your joystick movement. If this solved it, up vote it so others who have the problem will know that this is a plausible solution.
No it doesnt work... maybe I imported your Script wrong have a look http://jsfiddle.net/$$anonymous$$aru07/mzGyh/