Advertisement
Guest User

if statement not working correctly with keyinput

a guest
Jul 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. public bool upKeyReleased = false;
  2. public bool downKeyReleased = false;
  3. public bool leftKeyReleased = false;  // not currently used
  4. public bool rightKeyReleased = false; // not currently used
  5. public bool upKeyPressed = false;
  6. public bool downKeyPressed = false;
  7. public bool leftKeyPressed = false;   // not currently used
  8. public bool rightKeyPressed = false;  // not currently used
  9. public bool jumpKeyPressed = false;
  10.  
  11. public float speedF = 5;
  12. public float speedFF = 1.8F;
  13. public float speedB = 1.8F;
  14. public float TurnSpeed = 200;
  15. public float JumpSpeed = 10;
  16. public float i;
  17. public bool runF;
  18. public bool runB;
  19. public bool fall;
  20. public bool MayJump;
  21. public bool startedCounting;
  22. public bool teleported;
  23. public bool Jump;
  24. public bool death;
  25. public bool first;
  26. public Transform Player;
  27. public Rigidbody PlayerRagdoll;
  28. public Animator anim;
  29. public int CollisionCount = 0;
  30. public Slider slider;
  31. public AudioSource walksound;
  32. private Rigidbody rb;
  33.  
  34.  
  35. // Use this for initialization
  36. void Start()
  37. {
  38.     rb = GetComponent<Rigidbody>();
  39.     startedCounting = false;
  40.     death = false;
  41.     slider = Slider.FindObjectOfType<Slider>();
  42.     AudioSource walksound = GetComponent<AudioSource>();
  43. }
  44.  
  45. void Update()
  46. {
  47.  
  48.    
  49.  
  50.  
  51.     if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
  52.     {
  53.         upKeyReleased = true;
  54.         upKeyPressed = false;
  55.     }
  56.     else if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
  57.     {
  58.         downKeyReleased = true;
  59.         downKeyPressed = false;
  60.     }
  61.     else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
  62.     {
  63.         leftKeyReleased = true;
  64.         leftKeyPressed = false;
  65.     }
  66.     else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
  67.     {
  68.         rightKeyReleased = true;
  69.         rightKeyPressed = false;
  70.     }
  71.     else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  72.     {
  73.         upKeyPressed = true;
  74.         upKeyReleased = false;
  75.     }
  76.     else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  77.     {
  78.         downKeyPressed = true;
  79.         downKeyReleased = false;
  80.     }
  81.     else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  82.     {
  83.         leftKeyPressed = true;
  84.         leftKeyReleased = false;
  85.     }
  86.     else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  87.     {
  88.         rightKeyPressed = true;
  89.         rightKeyReleased = false;
  90.     }
  91.  
  92.     if (upKeyReleased || fall)
  93.     {
  94.         walksound.Stop();
  95.     }
  96.  
  97.     else if (upKeyPressed && (!walksound.isPlaying))
  98.     {
  99.         walksound.Play();
  100.         walksound.loop = true;
  101.     }
  102.  
  103.  
  104.     //if moving set run true
  105.     if (upKeyPressed && !runB)
  106.     {
  107.         runF = true;
  108.     }
  109.     else
  110.     {
  111.         runF = false;
  112.     }
  113.  
  114.     if (downKeyPressed && !runF)
  115.     {
  116.         runB = true;
  117.     }
  118.     else
  119.     {
  120.         runB = false;
  121.     }
  122.  
  123.  
  124.     // if not running running foreward animation and not falling
  125.     if (runF && !runB && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
  126.     {
  127.         anim.Play("run", 0, 0f);
  128.         print("running");
  129.     }
  130.     if (runB && !runF  && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("back") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
  131.     {
  132.         anim.Play("back", 0, 0f);
  133.     }
  134.     if (Input.GetKey(KeyCode.Space) && MayJump == true)
  135.     {
  136.         Jump = true;
  137.     }
  138.     else
  139.     {
  140.         if (CollisionCount != 0)
  141.         {
  142.             Jump = false;
  143.         }
  144.     }
  145.  
  146.  
  147.     slider.value -= Time.deltaTime;
  148.     // if not running play idle animation
  149.     if (!fall && !runB && !runF && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("idle"))
  150.     {
  151.         anim.Play("idle", 0, 0f);
  152.     }
  153.     if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("idle"))
  154.     {
  155.         fall = false;
  156.     }
  157.     if(CollisionCount == 0)
  158.     {
  159.         StartCoroutine("WaitSeconds");
  160.         if (startedCounting && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall") && fall)
  161.         {
  162.             anim.Play("fall", 0, 0f);
  163.         }
  164.     }
  165.     else
  166.     {
  167.         startedCounting = false;
  168.     }
  169.     //if no collsion play falling animation
  170.     if (CollisionCount > 0)
  171.     {
  172.         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
  173.         {
  174.             fall = false;
  175.             anim.Play("idle", 0, 0f);
  176.         }
  177.     }
  178.  
  179.  
  180.     if (GameObject.Find("Stick_Figure_T-pose ragdoll(Clone)"))
  181.     {
  182.         Destroy(gameObject);
  183.     }
  184.     if (slider.value == 0)
  185.     {
  186.         MayJump = false;
  187.     }
  188.     else
  189.     {
  190.         MayJump = true;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement