- Home /
 
Help with player script.
I have been working on a crouch function for my 2d side scroller. It works,but it stops the attack function from working. Also I have tried to add jumping in there, but I couldn't figure it out. Could some one please help me?
 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
     
 public class player : MonoBehaviour {
     
     
     
     public float speed = 1.0f;
     public float jump = 10.0f;
     public float gravity = 10.0f;
 
         
     private Transform thisTransform;
     private Sprite sprite;
     private bool flipped;
 
     // Use this for initialization
     void Awake () {
         thisTransform = transform;
         sprite = (Sprite)GetComponent(typeof(Sprite));
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 moveVector = new Vector3(0,0,0);
         bool moving = false;
         
         
         
     if(Input.GetKey(KeyCode.A)){
             moveVector.x -= 1.0f;
             moving = true;
         
     }
         
         if(Input.GetKey(KeyCode.D)){
             moveVector.x += 1.0f;
             moving = true;
             
             
         }
         
 
         if(Input.GetKey(KeyCode.Mouse0)){
             Attack();
         } else {
             if(!IsAttacking()) {
                 if(moving) {
                     Move(moveVector);
                 } else {
                     Stand();
             }
         }
         }
         if(Input.GetKey(KeyCode.S)){
             Crouch();
         } else {
             if(!IsCrouching()) {
                 if(moving) {
                     Move(moveVector);
                 } else {
             Stand();
     }
     }
         }
     }
         
     
     
 
     
     private void Stand() {
         sprite.Stop();
     }
     
     
     private void Move(Vector3 moveVector) {
         FlipSprite(moveVector.x);
         moveVector *= speed * Time.deltaTime;
         thisTransform.position = thisTransform.position + moveVector;
         sprite.Play("walk");
         
         
         
     
     }
     
     private void FlipSprite(float x) {
         if(x == 0.0f) return;
         
         float xDir = Mathf.Sign(x);
         if(!flipped && xDir < 0.0f) {
             flipped = true;
             sprite.SetFlippedState(true, false);
         } else if(flipped&& xDir > 0.0f) {
             flipped = false;
               sprite.SetFlippedState(false, false);
         }
     }
     
     private void Attack() {
         if(IsAttacking()) return;
         
         sprite.Play("Sword");
     }
     
     private bool IsAttacking() {
         if(sprite.IsAnimationPlaying("Sword")) return false;
         return false;
     
 
 }
     
     private void Crouch() {
         if(IsCrouching()) return;
         
         sprite.Play("crouch");
     }
     
     private bool IsCrouching() {
         if(sprite.IsAnimationPlaying("crouch")) return false;
         return false;
     
 
     
     
 
     
         
     
 }            
         
     
     
 
 
 }
 
              Answer by ShadoX · Sep 05, 2013 at 10:18 AM
The problem seems to be with your if:
   if(Input.GetKey(KeyCode.S)){
      Crouch();
    } else {//problem starts here
      if(!IsCrouching()) {
       if(moving) {
           Move(moveVector);
       } else {
      Stand();
 }
 
               You check if the player is pressing "s" and if he is - the char crouches , if he isn't - you move (if moving == true).. you probably want to move the following part outside the "else" part
  if(moving) {
      Move(moveVector);
  }
 
              That script now allows me to attack with the crouch added in, but I don't want him to move while crouched is there a way to do that?
Sorry, can't believe I missed that part.. in that case please ignore what I said about the if. Try having a look at http://answers.unity3d.com/questions/160848/how-do-i-run-two-animations-at-once-on-the-same-ga.html Initially I thought that you might want to handle the player input differently, but I guess that animation blending also should work.
@nasby321: If it solved your problem, then don't forget to mark this question as "answered".
Your answer
 
             Follow this Question
Related Questions
Enemy follows the only player in rotation 2 Answers
Why do I double jump? This isn't supposed to happen... 2 Answers
Player wont jump (visual script),2d player does not jump (visual script) 1 Answer
2D Level Design (programmatically or not) 1 Answer
Need help with 2D Attack, attacking in every direction 0 Answers