- Home /
How to change 'vertical' and 'horizontal' axis when rotating the object
Hello there :)
I'm trying to create a charcter controller similiar to the 'ALIEN SWARM 2k4' and I want it to change vertical and horizontal axis animations when I rotate it by 90 degrees(i.e. strafe right change to walk forward when rotate character 90 degrees right ect.).
For now it always keep the same axis when rotated. Anyone can help me with this? THANKS!
Code:
#pragma strict
var runSpeed:float;
var walkSpeed:float;
var walkbackSpeed:float;
var strafeSpeed:float;
var rotationSpeed:float;
var gravity:float;
private var charController:CharacterController;
private var walking:boolean;
private var strafing:boolean;
private var moveDirection : Vector3 = Vector3.zero;
function Start(){
charController = GetComponent(CharacterController);
}
function Update () {
if(Input.GetAxis("Vertical") > 0){
walking = true;
if(Input.GetButton("Run")){
charController.Move(transform.forward * runSpeed * Time.deltaTime);
}
else{
animation.CrossFade('TechnicWalk');
charController.Move(transform.forward * walkSpeed * Time.deltaTime);
}
}
else if(Input.GetAxis("Vertical") < 0){
walking = true;
if(Input.GetButton("Run")){
charController.Move(transform.forward * -runSpeed * Time.deltaTime);
}
else{
animation.CrossFade('TechnicWalkBack');
charController.Move(transform.forward * -walkbackSpeed * Time.deltaTime);
}
}
else{
walking = false;
}
if(Input.GetAxis("Horizontal") > 0){
strafing = true;
animation.CrossFade('TechnicStrafe');
charController.Move(transform.right * strafeSpeed * Time.deltaTime);
}
else if(Input.GetAxis("Horizontal") < 0){
strafing = true;
animation.CrossFade('TechnicStrafe');
charController.Move(transform.right * -strafeSpeed * Time.deltaTime);
}
else{
strafing = false;
}
if(!walking && !strafing){
animation.CrossFade('TechnicIdle');
}
if (charController.isGrounded) {
moveDirection = transform.TransformDirection(moveDirection);
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * Time.deltaTime);
transform.Rotate(Vector3(0, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime, 0));
}
Comment