- Home /
Locking z access causes a stuck character
I have a script in which I am attempting to lock a 3D character so that movement is only possible in the x any y axis (only x is really important though). I am using a character controller so the usually method using a configurable joint doesn't seem to work.
I made the following script, but when I use any of the commented out lines at the bottom, it just locks the character in position on all axes.
Been pulling my hair out over this for a while. Any help would be appreciated.
#pragma strict
var moveSpeed :float = 2;
var rotateSpeed :float = 30;
var facing: float = 1;
var zaxis: float = 0;
function Start () {
zaxis = transform.position.z;
}
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var king = GameObject.Find("SpartanKing"); // Get the model
var h = Input.GetAxis("Horizontal");
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * moveSpeed * h); //possibly remove facing when you fix the camera
//Animate
if (Input.GetKeyDown("space"))
{
king.animation.Play("attack");
}
else
{
if (h == 0) // Standing Still case case
{
king.animation.Play("idle");
}
else // Moving cases
{
if (h != facing) // turn around case
{
facing = h;
if (h < 0)
king.transform.eulerAngles = Vector3(0,270,0);
else
king.transform.eulerAngles = Vector3(0,90,0);
}
king.animation.Play("run");
}
}
// Attempted solutions.
//controller.transform.position.z = zaxis;
//transform.position.z = zaxis;
//transform.position.z = 1000;
//transform.position = new Vector3(transform.position.x, transform.position.y, zaxis);
}
Comment
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Move Object Smoothly 1 Answer
How to make basic AI in a 2d game? 4 Answers
Play animation for horizontal and vertical movement? 1 Answer
Scripting object movement? 1 Answer