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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How can I avoid using the Update function in this example?

Discussion in 'Scripting' started by fazraz, Jan 24, 2018.

  1. fazraz

    fazraz

    Joined:
    Feb 27, 2015
    Posts:
    14
    This script control the player movement. It sends the player along the X axis at a constant velocity and when you tap the screen, it moves the player down and if you tap it again it moves the player back up. How can I optimize this as much as possible by not using the Update function?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class PlayerMovement : MonoBehaviour {
    7.  
    8.     private float moveSpeed;
    9.     private bool isUp;
    10.  
    11.     void Start(){
    12.         isUp = true;
    13.         moveSpeed = 8;
    14.     }
    15.  
    16.     void Update () {
    17.         transform.Translate (moveSpeed * Time.deltaTime, 0f, 0f);
    18.         Dip ();
    19.     }
    20.  
    21.     public void Dip(){
    22.         if (isUp) {
    23.             if (Input.GetMouseButtonDown (0)) {
    24.                 moveSpeed += 0.1f;
    25.                 transform.DOLocalMoveY (-1f, 0.2f, false);
    26.                 isUp = false;
    27.             }
    28.         } else if (!isUp) {
    29.             if (Input.GetMouseButtonDown (0)) {
    30.                 moveSpeed += 0.1f;
    31.                 transform.DOLocalMoveY (1f, 0.2f, false);
    32.                 isUp = true;
    33.             }
    34.         }
    35.     }
    36. }
    Many thanks for any help you can offer

    Farid
     
    Last edited: Jan 24, 2018
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. fazraz

    fazraz

    Joined:
    Feb 27, 2015
    Posts:
    14
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why do you think it's not "optimal" to use update? if you are using translate for "constant" motion you're going to need to do something every frame...

    the only issue I can see is you're repeated checks for the input, I'd probably move that check to the update call and call dip from there
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh and the else if is a little redundant, if a bool isn't true, its false, why check
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Use code tags please, as outlined in the sticky thread at the top of every forum.

    If you need to move an object manually every frame, then you need to get called every frame to do it. You could, and probably should (at least, I prefer it), move all of your input checks to a single "input handler" class and generate events that other scripts can add listeners for. In that case, PlayerMovement would just have a function it registers to receive "mouse button down" events and respond appropriately when those events are received.

    That doesn't really save you any performance when compared to the single script here though, it's just about efficiency when dealing with dozens of scripts all looking for inputs. If this object needs to be moved every frame, there's not really a more efficient way to do that. You can remove the Update entirely (assuming another script handles input) and create a coroutine for motion that stop when the object becomes idle again, but you're trading a trivial processing cost each frame for startup costs for the coroutine each time you need it, which would be often. That's more efficient for occasional events, but not ones that are needed near-constantly.
     
  7. fazraz

    fazraz

    Joined:
    Feb 27, 2015
    Posts:
    14
    Can I call the Dip function anywhere other than from inside the Update function?
     
  8. fazraz

    fazraz

    Joined:
    Feb 27, 2015
    Posts:
    14
    I'm not so worried about the constant movement, I understand that needs to be done every frame. I'm more concerned about the Update function continuously checking to see if you tap the screen
     
  9. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Don't worry about it, just do it in update, you will either be checking for input in this update loop, or you would be doing it in a other update loop and invoking events. So keep it simple, no point trying to optimize till you have a need to do so.