- Home /
 
 
               Question by 
               aaronrdmkr · Oct 12, 2014 at 02:49 PM · 
                2dmovementmovectrl  
              
 
              "sneak mode" (ctrl key) stops my movement.
So here's my code. The player is going to be able to sneak. I have the movement in but the sneaking right now is just a slower movement and bool showing if its reading that it is now in sneak mode. But if I hold the ctrl key (before or during movement) and change direction, all movement stops. It'll start again if I jump or release the movement and ctrl keys
using UnityEngine; using System.Collections;
     public class PlayerControl : MonoBehaviour
     {
         [HideInInspector]
         public bool jump = false;                            // Condition for whether the player should jump.
         public bool moving = false;
         public bool crouch = false;
         public bool hardToSee = false;
         public bool sneaking = false;
         public bool interact = false;                        //Condition for whether the PlayerPrefs can interact
         public float moveForce = 200;                        // Amount of force added to move the player left and right.
         public float speed;                                    // Speed the player will run depending on state
         private float runSpeed = 12f;                        // The running speed the player can travel in the x axis.
         private float sneakSpeed = 4f;                        // The sneaking speed the player can travel in the x axis.
         public float jumpForce = 2000;                        // Amount of force added when the player jumps.
         public Transform graphics;
         public float pushPower = 2;                            //
         public Transform lineStart, lineEnd, groundCheck;     //lineCast points to determine ground and interactables/enemies.
         
                 
         private bool grounded = false;                        // Whether or not the player is grounded.
         
 
         public SkeletonAnimation skeletonAnimation;
         Quaternion goalRotation = Quaternion.identity;
         RaycastHit2D whatIHit;
         
 
         string currentAnimation = "";
 
         
         void Awake()
         {
             
             
 
         }
         
         
         void Update()
         {
         Raycasting ();
         Movement ();
 
 
 
 
         Debug.DrawLine (lineStart.position, groundCheck.position);
 
 
         // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
         grounded = Physics2D.Linecast (lineStart.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
 
                             
         // If the jump button is pressed and the player is grounded then the player should jump.
         jump = (Input.GetButtonDown ("Jump") && grounded == true); 
 
         // If the ctrl button is pressed and the player is grounded then the player should crouch.
         crouch = (Input.GetKey (KeyCode.LeftControl) && grounded);
 
 
 
         }
 
 
         
     void Raycasting(){
 
 
         if (Physics2D.Linecast(lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer("Enemies"))){
             whatIHit = Physics2D.Linecast(lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer("Enemies"));
             interact = true;
         } else{
             interact = false;
         }
 
         if ((Input.GetKeyDown (KeyCode.E)) && interact == true) {
             Destroy(whatIHit.collider.gameObject);
                 }
 
     }
     
     
         void Movement ()
         {
 
                 // Cache the horizontal input.
                 float h = Input.GetAxis ("Horizontal");
         
                 // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
                 if (h * rigidbody2D.velocity.x < speed) 
                     //... add a force to the player.
                     rigidbody2D.AddForce (Vector2.right * h * moveForce);
                                     
 
 
                 // If the player should jump...
                 if (jump == true){
 
                         // Add a vertical force to the player.
                         rigidbody2D.AddForce(new Vector2 (0f, jumpForce));
                         
                         // Make sure the player can't jump again until the jump conditions from Update are satisfied.
                         jump = false;
                         
                 }
 
 
 
                 // Based on input, run animation and rotate Graphic when changing direction.                         
                 if (h > 0) {                        
                         moving = true;            
                         goalRotation = Quaternion.Euler (0, 0, 0);
                         ;
                 } else if (h < 0) {
                         
                         moving = true;
                         goalRotation = Quaternion.Euler (0, 180, 0);
                 } else  SetAnimation ("Idle", true);
                         moving = false;
                         
 
         graphics.localRotation = Quaternion.Lerp (graphics.localRotation, goalRotation, 5 * Time.deltaTime);
 
         if (sneaking == true) {
             speed = sneakSpeed;
         }else speed = runSpeed;
 
 
 
         if (crouch == true)
             sneaking = true;
         else if (crouch == false)
             sneaking = false;
 
     }
 
 
 
 
         
     void SetAnimation (string name, bool loop){
         if (name == currentAnimation)
             return;
         
         skeletonAnimation.state.SetAnimation (0, name, loop);
         currentAnimation = name;
     
 
     }
 }
 
              
               Comment
              
 
               
              Your answer