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

(Can't post on Answers so...)GameObject Instantiate lag on Input, spawns multiple at same time

Discussion in '2D' started by vereonis, Nov 7, 2017.

  1. vereonis

    vereonis

    Joined:
    Nov 4, 2017
    Posts:
    3
    Seeing as whenever I try and ask a question on Answers clicking "Post" just takes me to my account page, and as none of the posts on here about this issue have been addressed I'll have to ask here....

    Top down 2D Game, I want to fire a projectile from player on mouseclick to go direction player sprite is facing (player sprite can face any direction not locked to just 90 degree movement), I spawn in the projectile prefab using:

    Code (csharp):
    1. if (Input.GetMouseButtonDown(0))//Spawns Projectile
    2.         {
    3.             Projectiles.Add((Instantiate(projectilePrefab, transform.position, transform.rotation)));
    4.         }
    If I keep clicking the mouse while watching the Hierarchy delays appear in spawning, I'll click but nothing will be "spawned" then it'll add 2 to the Hierarchy at once, this can happen for up to 4, I'll do 4 quick clicks in a row, nothing will spawn on the click but a split second after it'll add 4 all at once. This is of course an issue as it means 2-4 projectiles are being fired on top of each other.

    This can happen even if I'm clicking fairly steadily and not very rapidly.


    Just for the sake of completion, bellow is the code I use to actually make the projectile move, in case any solutions are drastic and may cause this to also require updating. (Any suggestions in streamlining all this is of course welcome)

    Code (csharp):
    1. for (int i = 0; i < Projectiles.Count; i++)//Fires Projectile
    2.         {
    3.             GameObject goBullet = Projectiles[I];
    4.             if (goBullet != null)
    5.             { goBullet.transform.Translate(new Vector3(0, 1) * Time.deltaTime * projectileSpeed); }
    6.         }{/CODE][/I]
     
    Last edited: Nov 7, 2017
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  3. vereonis

    vereonis

    Joined:
    Nov 4, 2017
    Posts:
    3
    Ah, there was no preview when posting I thoguht doing 4 spaces like on Answers and Stack Overflow would make it "code", I've added tags.

    I moved my shooting code to its own script to make it less messy to post more detail and in doing so found my problem...

    I was previously calling the shoot code inside a fixedupdate, and when I moved it I left it in an normal void Update() which fixed the issue, I didn't think the regular update time of fixedupdate would have caused the issue I was witnessing.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I don't know exactly why you saw the behavior you did, but a good rule of thumb: always get your inputs in Update. FixedUpdate should only be used to update physics.