Search Unity

Laser blast shot how to delete after period of time and spaw another one

Discussion in 'Scripting' started by xXrandomryzeXx, Aug 6, 2018.

  1. xXrandomryzeXx

    xXrandomryzeXx

    Joined:
    Jul 31, 2018
    Posts:
    13
    for 3D game...yeah I know its a long title but my question is about how to create a clone of my prefab blast on mouse click or space bar press and after a period of time how to remove it so it doesn't mess with the cpu and create lots of lag after a decent amount of shots. Here is the code I have for that(oh and the way I tested it I moved it from my prefab folder when playing and pasted it on the game object of my blaster and se here the code is) the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class BlastShot : MonoBehaviour {
    7.  
    8.     public GameObject original;
    9.     public float BlastSpeed = 300;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         transform.Translate(transform.forward * BlastSpeed * Time.deltaTime);
    20.     }
    21. }
    22.  
     
    Last edited: Aug 6, 2018
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
  3. xXrandomryzeXx

    xXrandomryzeXx

    Joined:
    Jul 31, 2018
    Posts:
    13
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Being 2D or 3D has nothing to do with object pooling or Destroying. If you are spawning lots and lots of objects and having them all be destroyed can created lag itself, which is why we do object pooling.

    When you say you want to "spawn" a new objects, exactly why and what do you mean? Spawn can be allocate, or it can mean recycle.
     
  5. xXrandomryzeXx

    xXrandomryzeXx

    Joined:
    Jul 31, 2018
    Posts:
    13
    by that I mean I want to create it on the game object the blaster is.
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    2D / 3D and object pooling / instantiating & destroying are still not related to this.

    All you'd have to do in this case is just create a script for your blaster that contains a reference to your shot prefab, and assuming you are not using any object pooling, just set the shot's position = to the blaster's position when instantiated, then use the Destroy method with the optional time parameter to destroy the object after a certain amount of time.
    For example:
    Code (CSharp):
    1. public class Blaster : MonoBehavior {
    2.  
    3.    public GameObject blastShot;
    4.    private Transform thisTransform;
    5.  
    6.    void Start() {
    7.       thisTransform = GetComponent<Transform>(); //The Transform component on this GameObject
    8.    }
    9.  
    10.    void Update() {
    11.       //MouseButtonDown(0) == left click
    12.       if(Input.GetMouseButtonDown(0)) {
    13.          GameObject blastShotInstance = Instantiate(blastShot);
    14.          blastShotInstance.transform.position = thisTransform.position;
    15.          Destroy(blastShotInstance, 1); //This will destroy the blast shot instance after 1 second. Just change the number to however long you want the shot instance to stay.
    16.       }
    17.    }
    18. }
     
  7. xXrandomryzeXx

    xXrandomryzeXx

    Joined:
    Jul 31, 2018
    Posts:
    13
    thanks a lot , although I found a way to do it just had to spin some thing cuz the blast was facing up and then I had no problems, and the video is here if you want to check it out(its not mine):