- Home /
Unity Strafing Script
I am looking for a script that will allow the character to run and jump, but not rotate. Instead I want the character to be able to move across the screen while still facing forward. Can anyone help please?
Answer by aldonaletto · Jan 29, 2012 at 08:21 PM
Use the example script from CharacterController.Move: it moves left/right/front/back and jumps, but do not rotate. NOTE: This script is intended to be used with a CharacterController, obviously.
Answer by Davoid ZalaX · Dec 06, 2014 at 06:41 AM
Here, Try borrowing from this 3rd person character controller.
 #pragma strict
 
 var rotataionSpeed : float = 10;
 var walkSpeed : float = 7;
 var gravity : float = 50;
 private var yRot : float;
 var body : Transform;
 
 
 function Update () {
     var Controller : CharacterController = GetComponent(CharacterController);
     var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
     var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
     var height : Vector3 = transform.TransformDirection(Vector3.up);
     
     if(Input.GetKeyDown("space")){
     Jump();
     }
     
 if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
     animation.CrossFade("walk", 0.2);
     animation["walk"].speed = walkSpeed/10;
     Controller.Move((vertical*(walkSpeed*Input.GetAxis("Vertical")))*Time.deltaTime);
     Controller.Move((horizontal*(walkSpeed*Input.GetAxis("Horizontal")))*Time.deltaTime);
 }else{
     animation.CrossFade("idle", 0.2);
 }
 
 if(Input.GetAxis("Mouse X")){
     yRot += 10*Input.GetAxis("Mouse X");
     }
     transform.rotation = Quaternion.Euler(0, yRot, 0);
     
     Controller.Move(height*gravity*Time.deltaTime);
 }
 function LateUpdate(){
  // Rotate the Character to match the direction he/she is going
   if(Input.GetAxis("Vertical") == 0){
     if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = 180;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = 0;
     }
   }else if(Input.GetAxis("Vertical") > 0){
     if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = 135;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = 45;
     }
   }else if(Input.GetAxis("Vertical") < 0){
     if(Input.GetAxis("Horizontal") == 0){
       body.localEulerAngles.y = -90;
     }else if(Input.GetAxis("Horizontal") > 0){
       body.localEulerAngles.y = -135;
     }else if(Input.GetAxis("Horizontal") < 0){
       body.localEulerAngles.y = -45;
     }
   }
 }
 function Jump(){
     animation.CrossFade("jump_pose", 0.2);
     gravity = 3;
     yield WaitForSeconds(0.5);
     gravity = -10;
 }
 
Your answer
 
 
             Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Player slows down when hitting an incline 0 Answers
C# Movement Script 3 Answers
Kinect issue with Character Movement while in flight 0 Answers
Network - Character Controls 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                