Search Unity

I think my if statement is long-winded

Discussion in 'Scripting' started by kittik, Jul 31, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    I am trying to think of a nice way of putting:
    Code (CSharp):
    1. GetMouseDown(0) AND (KeyCode.LeftShift OR KeyCode.RightShift)
    The way I am writing it doesn't feel optimised to me. Would you write your if statement this way, or use something different?
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Anyway is fine, it won't affect performance. Don't think of nice ways to 'put' things, just do it. If you need to optimise, you do that after when you actually know you need to.
     
    kittik likes this.
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thanks, I'll use that then. Is there a way of stating I want shift, but I don't care which shift it is?
     
  4. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    I just tried:

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0) && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)))
    I can't quite see why this isn't working. The code is fine (according to Unity), but I think I might have missed a bracket. Any ideas?
     
  5. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    ^ Nevermind, everything does work. I mistook my console.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you are worried about typing this out over and over again, push it into a static helper method.
     
    kittik likes this.
  7. lede701

    lede701

    Joined:
    Mar 22, 2013
    Posts:
    2
    Actually this is very small compared to some if I have had to write in the past. It does look optimizes though because if the mouse button is false then the if statement will be skipped and the two shift checks are not evaluated.

    For now I wouldn't worry about how many CPU cycles this if takes because at this point it is a mute point.
     
    kittik likes this.