Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Needing some minor assistance & fresh eyes.

Discussion in 'Scripting' started by Quincey, Apr 1, 2020.

  1. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Hello all,

    I'm not quite sure what the issue is with my code I've tried numerous debugs and for some strange reason I can't get the last "If-Statement" to fire in the (Input.GetMouseKey) function. I'm not sure if code has a limit on how many "if-statements" you can use or if I'm doing something slightly wrong. For the most part 95% of my code works could anyone who's up to reviewing my code to throw me some hints I'd be greatly appreciative. I've included my code, as well as, a screenshot to showcase the issue with the last if-statement not firing in the editor.

    V/r,

    Another Unity User :cool: Screen Shot 2020-03-31 at 11.22.08 PM.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChargedAttack : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public bool isCharging;
    10.     public bool chargedUp;
    11.     public float buttonPress;
    12.     public float maxButtonPress = 5f;
    13.     public Animator anim;
    14.     public Rigidbody rigid;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         anim = GetComponent<Animator>();
    21.         rigid = GetComponent<Rigidbody>();
    22.         anim.SetBool("isIdling", true);
    23.     }
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.         if (Input.GetMouseButton(1))
    31.         {
    32.             isCharging = true;
    33.             Debug.Log("Charging up...!");
    34.  
    35.             buttonPress += Time.deltaTime;
    36.  
    37.               if (buttonPress < maxButtonPress)
    38.               {
    39.                   buttonPress += Time.deltaTime;
    40.  
    41.                     if (buttonPress == 5f)
    42.                     {
    43.                         chargedUp = true;
    44.                         buttonPress = maxButtonPress;
    45.                         Debug.Log("CHARGED UP!");
    46.                         anim.SetTrigger("Attack5");
    47.                     }
    48.               }
    49.         }
    50.  
    51.  
    52.         if (Input.GetMouseButtonUp(1))
    53.         {
    54.             isCharging = false;
    55.             Debug.Log("Charging down...!");
    56.             buttonPress = 0f;
    57.             anim.SetBool("isIdling", true);
    58.         }
    59.  
    60.     }
    61. }
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Code (CSharp):
    1. if (buttonPress == 5f)
    The computer will check to see if your timer (buttonPress) is precisely 5 (not "about" or "close to" 5). The odds of your timer being exactly and precisely 5 is very low, and as such your timer will just keep counting up beyond 5 and the condition will never become true.

    You can see this by adding a Debug.Log(buttonPress) and observing that it doesn't actually ever become exactly 5, it will most likely skip over that.

    What you probably want to do is check if your timer has exceeded 5 instead. Since you are already checking if it's smaller than 5 anyway, you can write your code as such:

    Code (CSharp):
    1.               if (buttonPress < maxButtonPress)
    2.               {
    3.                   buttonPress += Time.deltaTime;
    4.               }
    5.               else
    6.               {
    7.                     chargedUp = true;
    8.                     buttonPress = maxButtonPress;
    9.                     Debug.Log("CHARGED UP!");
    10.                     anim.SetTrigger("Attack5");
    11.                 }
    12.           }
     
  3. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Bob,

    I'll revamp my code I wasn't sure if there was a limit on if-statements or where I was going wrong. I really appreciate the knowledge and know I'm better prepared if and when I need to use this logic later on in the future. Huge thanks again for your time.
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    No problem.

    For reference, there is no limit on if-statements. Some (poorly-written) code I have seen has had dozens of nested if-statements.
     
    Quincey likes this.
  5. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46

    I'm teaching myself Unity and learning C# by watching all the Unity 2019 basic tutorials..You slightly stepped on my toes about the poorly written code lol. I'm trying to get out of the habit of writing so many if-statements I'm trying to learn how to apply better logic and find ways to condense my code. I'm slowing learning and thank God for this forum and friendly people willing to share advice and give good insight.
     
    bobisgod234 likes this.