- Home /
 
               Question by 
               darkal · Jul 22, 2014 at 06:35 PM · 
                javascriptmovementplayerboolean  
              
 
              auto run key
the code works fine and all but im trying 2 add a auto run into it but im not sure what i need 2 add into the code to allow it
 var speed : float = 6.0;
     var increase = 0;
     var jumpSpeed : float = 8.0;
     var gravity : float = 20.0;
     var script : speed;
     var autorun : boolean = false;
     private var moveDirection : Vector3 = Vector3.zero;
 
     function Update() 
     {
         script = this.GetComponent("speed");
         speed = script.speed;
         
         var controller : CharacterController = GetComponent(CharacterController);
         if (controller.isGrounded) {
             // We are grounded, so recalculate
             // move direction directly from axes
             moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                     Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
             if (Input.GetKey(KeyCode.LeftShift))
             {
                 if (Input.GetKey(KeyCode.W))
                 {
                     autorun = !autorun;
                 }
             }
             
             if(autorun)
             {
                 
             }
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
         
         // Move the controller
         controller.Move(moveDirection * Time.deltaTime);
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Razacx · Jul 22, 2014 at 06:43 PM
You'd do the exact same as in the part that handles the movement with input:
 if (autorun && controller.isGrounded) {
             // We are grounded, so recalculate
             // move direction
             moveDirection = Vector3(0, 0,
                                     1);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed; 
         }
The only thing that changes is that instead of checking for input in moveDirection, you give the input yourself.
Your answer
 
 
             Follow this Question
Related Questions
Player movement isn't working 1 Answer
Making my look script turn smoothly 0 Answers
Move RigidBody character relative to camera. 2 Answers
Slow down character while reloading. 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                