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

Getting Input every few frames rather than every frame through void Update

Discussion in 'Scripting' started by AreebM, Sep 15, 2014.

  1. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    Hi,

    Currently, I have a cannon in my scene, which fires bullets once the user touches it. The bullets instantiate and fire, but the problem I'm having is that when I touch the cannon, multiple frames pass as I'm touching it and so many bullets instantiate on top of each other.

    According to my script, when the bullets touch different objects, the scores and lives update in various ways and the bullets disappear. Now, when the bullets touch each other, my scores and lives are going berserk and nothing in the scene is being destroyed.

    So the question is, is there any way to limit Instantiation or to get an update every few frames so that the bullets don't instantiate on top of each other?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,503
    You can make a member variable float called "gunheat" and set it to a value in seconds when you fire a bullet, then count it down with Time.deltaTime, and disregard all input until it becomes zero again.

    That will make your gun only fire once per the time value you use.

    But you should also learn about and use layers to determine what collides with what. Hie thee to the documentation on unity's layer system!

    Kurt
     
    AreebM likes this.
  3. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You could e.g.
    • use a timer, so that the cannon can only shoot once per second
    • only react to Taps instead of simply using the touches position(-> Touch Phase ended and time between beginning and ending of touch max. 0.3 seconds)
    • set a bool while touch is avaiable and bullet has been shot to avoid it to be fired again while the same touch is active.
     
    AreebM likes this.
  4. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I like the Taps idea, but I'm fairly new and don't know how to incorporate it.
    Here's my current script, could you point me in the right direction? I know Input.touches.Length needs to be changed but I had trouble enough figuring this out!

    Code (CSharp):
    1.     void FixedUpdate () {
    2.             if (Input.touches.Length > 0 && guiTexture.HitTest(Input.touches[0].position)){
    3.                 audio.Play();
    4.                 Instantiate...
    5.             }
    6.     }
     
  5. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Quick and dirty (can't test or code unity on this machine) so this isn't tested

    Code (CSharp):
    1. bool Tap(Touch t)
    2. {
    3.        if(t.phase == TouchPhase.Ended && t.deltaTime < 0.3f)
    4.                return true;
    5.         return false;
    6. }
    You just check for the Tap right before you instantiate.

    Something like this:

    Code (CSharp):
    1. void FixedUpdate () {
    2.             if (Input.touches.Length > 0 && guiTexture.HitTest(Input.touches[0].position) && Tap(Input.touches[0])){
    3.                 audio.Play();
    4.                 Instantiate...
    5.             }
    6.     }
    You can make the Tap code more complex and better if you also checked for movement of the finger (so that swipes don't fire the cannon), but you can work on that later
     
    Last edited: Sep 15, 2014
    AreebM likes this.