}//end Start // Update is called once per frame. Here, we are gathering player input. void Update() { /** * Vector2 means a vector in 2 space. Here, we create a new vector and use use the Input object in unity that reads key controls. * Key names and other controls are designated under Edit > Project Settings > Input. * There are two different versions of GetAxis we can use. * Input.GetAxis(String) has a smoother movement where the character gradually speeds up and slows after the key is let go. * Input.GetAxisRaw(String) has a snappier movement where the character reaches max speed as soon as the key is pressed * and stops immediately after the key is let go. */ Vector2 inputMovement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); if (currentHealth <= 0) { deathScreen.enabled = true; mp3.dead = true; } else { if (Input.GetAxisRaw("Horizontal") > 0 && Input.GetAxisRaw("Vertical") == 0) { animator.SetFloat("IdleHorizontal", inputMovement.x); animator.SetFloat("IdleVertical", inputMovement.y); animator.SetFloat("Horizontal", inputMovement.x); animator.SetFloat("Vertical", inputMovement.y); } else if (Input.GetAxisRaw("Horizontal") < 0 && Input.GetAxisRaw("Vertical") == 0) { animator.SetFloat("IdleHorizontal", inputMovement.x); animator.SetFloat("IdleVertical", inputMovement.y); animator.SetFloat("Horizontal", inputMovement.x); animator.SetFloat("Vertical", inputMovement.y); } else if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") > 0) { animator.SetFloat("IdleHorizontal", inputMovement.x); animator.SetFloat("IdleVertical", inputMovement.y); animator.SetFloat("Horizontal", inputMovement.x); animator.SetFloat("Vertical", inputMovement.y); } else if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") < 0) { animator.SetFloat("IdleHorizontal", inputMovement.x); animator.SetFloat("IdleVertical", inputMovement.y); animator.SetFloat("Horizontal", inputMovement.x); animator.SetFloat("Vertical", inputMovement.y); } else if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0) { animator.SetFloat("Horizontal", inputMovement.x); animator.SetFloat("Vertical", inputMovement.y); } /** * Here, we take our normal move vector and add a vector speed to it. However; an issue with this is that if we were to move diagonally, * like normal physics we would have a greater speed then we did moving side to side. Adding the normalized method fixes this and the * speed is kept constant. */ animator.SetFloat("Speed", inputMovement.sqrMagnitude); moveVelocity = inputMovement.normalized * speed; } if (Input.GetKeyDown(KeyCode.Space)) { TakeDamage(2); } if (previousHours != time.GetHours() && sleep == false && firstFrame == true) { GetHungrier(2); previousHours = time.GetHours(); healthRegen(currentFood); TakeDamage(deltaHealth); } if (currentFood < 0) { currentFood = 0; } else if (currentFood > 100) { currentFood = 100; } if (currentHealth < 0) { currentHealth = 0; } else if (currentHealth >= 101) { currentHealth = 100; } firstFrame = true; foodBar.SetFood(currentFood); healthBar.SetHealth(currentHealth); moneyCount.text = $"{gold}"; }//end Update