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. Dismiss Notice

Question on "If" statements affecting performance

Discussion in 'Scripting' started by Wyko, Jun 19, 2011.

Thread Status:
Not open for further replies.
  1. Wyko

    Wyko

    Joined:
    Jun 19, 2011
    Posts:
    10
    Hey there! This is my first post on this forum, although it's far from the last. I'm learning Unity and I'm loving the platform!

    I have a piece of code I am creating to govern a RTS-style scrolling camera, constrained to the bounds of the terrain. I have the mouse scrolling working, and now I'm working on the keyboard controls. My problem lies here.

    Code (csharp):
    1. transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );
    This is the basic code for the translation. It works smoothly and consistently, but by itself it doesn't respect the constraints I have on the edge of the map. So I'm trying to use the following code.

    Code (csharp):
    1. if (Input.GetAxis("Horizontal") == -1  (viewPosLT.x < 0)){transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );}
    2.  
    3.     else if (Input.GetAxis("Horizontal") == 1  (viewPosRB.x > 1)) {transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );}
    4.  
    5.     else if (Input.GetAxis("Vertical") == -1  (viewPosRB.y < 0)) {transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );}
    6.  
    7.     else if (Input.GetAxis("Vertical") == 1  (viewPosLT.y > 1)) {transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );}
    or this:

    Code (csharp):
    1. if ((Input.GetAxis("Horizontal") == -1  (viewPosLT.x < 0))
    2.     || (Input.GetAxis("Horizontal") == 1  (viewPosRB.x > 1))
    3.     || (Input.GetAxis("Vertical") == -1  (viewPosRB.y < 0))
    4.     || (Input.GetAxis("Vertical") == 1  (viewPosLT.y > 1))) {transform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime, Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );}
    viewPosLT and RB are Vector3's.

    The last two pieces of code cause a significant delay before movement happens, and the movement does not have a smooth start. How can I change this to make the motion as smooth as the basic code?
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If stuff doesn't cause any performance issues, even with the hundreds used in a* code, its not a bother. You are free to use a lot of ifs. You should easily have 500+ ifs on a mobile without framerate issues, so you need to look at what else could be causing these problems. The if statements here are 100% not causing your slowdowns.

    The problem lies here: if (Input.GetAxis("Horizontal") == -1

    You see, it doesn't move from -1 to 1 right away, it gradually moves there to simulate an analog pad. This setting can be changed in your project settings... as it won't reach =-1 for a while and will seem laggy I believe?

    You shouldn't be doing that code to clamp to the edge of the area anyway since it won't be accurate as it may overshoot a little bit.

    Instead:

    1. perform movement
    2. check bounds
    3. correct position to bounds if over

    You're lumping code up too much, which is causing bad logic, and it isn't actually providing any speed gains at all.
     
  3. Wyko

    Wyko

    Joined:
    Jun 19, 2011
    Posts:
    10
    That's perfect! I understood it to be a straight 1 - 0 - (-1) transition when you pressed the arrow keys. That explains everything. Thanks!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    You can change the input settings for axes so that it snaps to -1 / 1 without gradually ramping up. (Or make it even more gradual, if you need to.)

    --Eric
     
  5. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Instead of using Input.GetAxis which gives you a smoothed value, you can use Input.GetAxisRaw, then you get the raw values without any smoothing.
     
  6. SAMYTHEBIGJUICY

    SAMYTHEBIGJUICY

    Joined:
    Jul 12, 2023
    Posts:
    19
    THIS IS A GREAT THREAD THANK YOU EVERYONE I LOVE YOU ALL
     
  7. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Next time hit the Like button instead of reviving a 12 year old thread.
     
    Ryiah, Bunny83 and tsukimi like this.
Thread Status:
Not open for further replies.