using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerInput : MonoBehaviour { public Animator animator; public PlayerController pCont; public int maxJumps = 2; public int currentJumps = 0; public float jumpAdd = 2; //isCrouch prevents the animator from switching back to idle when character is crouching. bool isCrouch = false; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { float xDir = Input.GetAxis("Horizontal"); //if statement includes isCrouch to prevent switching to idle if crouching if(xDir == 0 && !isCrouch) { Debug.Log("idle"); //not moving; //all animator bools have to be set so that only one is true at a time! animator.SetBool("PIdle", true); animator.SetBool("PRun", false); animator.SetBool("PCrouch", false); } //else if includes !Input.GetButtonDown("Crouch") to keep character crouching and not switching to running else if (xDir != 0 && !Input.GetButtonDown("Crouch")) { //all animator bools have to be set so that only one is true at a time! animator.SetBool("PRun", true); animator.SetBool("PIdle", false); animator.SetBool("PCrouch", false); //Moving; } if (Input.GetKeyDown("j")) { animator.SetTrigger("PAttack"); } if(Input.GetButtonDown("Jump") && currentJumps < maxJumps) { pCont.JumpUnconditionally(); currentJumps++; animator.SetTrigger("PJump"); } if(pCont.landed) { currentJumps = 0; animator.SetTrigger("PLand"); } else { animator.ResetTrigger("PLand"); } if(!Input.GetButton("Jump") && !pCont.landed) { pCont.CancelJump(); } if(Input.GetButtonDown("Crouch")) { //set isCrouch to true to prevent switching to idle animation isCrouch = true; //all animator bools have to be set so that only one is true at a time! animator.SetBool("PCrouch", true); animator.SetBool("PIdle", false); animator.SetBool("PRun", false); pCont.Crouch(); } if(Input.GetButtonUp("Crouch")) { //set isCrouch to false to switch back to idle animation isCrouch = false; pCont.UnCrouch(); //all animator bools have to be set so that only one is true at a time! animator.SetBool("PCrouch", false); animator.SetBool("PIdle", true); animator.SetBool("PRun", false); } pCont.Move(xDir); } private void OnTriggerEnter2D(Collider2D collision) { if(collision.tag == "JumpBoost") { pCont.jumpForce += jumpAdd; } if(collision.tag == "Grow") { gameObject.GetComponent().localScale = new Vector3(1.5f, 1.5f, 0); } } }