- Home /
Relative Movement Problem
i'm trying to make a movement system where depending on the camera's angle the character moves forward back left and right relative to the camera. the player also looks in the direction it moves. it works fine except that if you tilt the camera on the x or z axis the player rotates the same way. I cant figure out how to limit the rotation of the player to only the y axis. here's my script:
@script RequireComponent(CharacterController)
var moveSpeed : float = 10;
var gravity : float = 10;
var jumpHeight : float = 4;
var jumpCount : int = 0;
var jumpCap : int = 2;
var moveDir : Vector3 = Vector3.zero;
var cam : Transform;
function Update () {
var cont : CharacterController = GetComponent(CharacterController);
if(cont.isGrounded) {
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
jumpCount = 0;
if(moveDir != Vector3.zero) {
moveDir = cam.TransformDirection(moveDir);
transform.localRotation = Quaternion.LookRotation(moveDir);
moveDir = transform.forward;
}
if(Input.GetButtonDown("Jump") && jumpCount <= jumpCap) {
jumpCount+= 1;
moveDir.y += jumpHeight;
}
moveDir *= moveSpeed;
}
moveDir.y -= gravity;
cont.Move(moveDir * Time.deltaTime);
}
Answer by aldonaletto · Jul 17, 2011 at 02:19 AM
Just zero the y coordinate after TransformDirection - it will keep the transformed direction in the ZX plane:
... moveDir = cam.TransformDirection(moveDir); moveDir.y = 0; // null Y coordinate transform.localRotation = Quaternion.LookRotation(moveDir); ...
Your answer
Follow this Question
Related Questions
Global movement 0 Answers
Camera relative movement 0 Answers
Camera Relative Movement 2 Answers
Camera/Direction Rotation 2 Answers
Help with my Character Controller 1 Answer