Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get boolean int value?

Discussion in 'Scripting' started by thomasotje10, Jun 18, 2018.

  1. thomasotje10

    thomasotje10

    Joined:
    Apr 6, 2015
    Posts:
    19
    Look here:
    emission.rateOverTime = Mathf.Abs(moveX) * 10 * isGrounded;

    I want to define the rateOverTime based on the horizontal player speed, but at the same time check weather the player is grounded or not. Offcourse i could just do this with a simple if statement but since im curious i'd like to know if there's a way if i can "convert" the true/false value of the boolean "isGrounded" to a simple 1 or 0 int or float value, so i can use something as simple as the above line of code (which obviously doesn't work right now).
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    emission.rateOverTime = isGrounded ? Mathf.Abs(moveX) * 10 : 0;


    the
    ?:
    operator is useful for inline evaluation branches like that. Nesting them gets ugly though, so keep it simple.
     
    Ian094 and Doug_B like this.
  3. thomasotje10

    thomasotje10

    Joined:
    Apr 6, 2015
    Posts:
    19
    Thank you! :) I totally forgot about those operators.