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

Apply FX at the objects pos?

Discussion in 'Scripting' started by cruising, May 13, 2015.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    In my enemy script i apply a exlposion FX when the enemy dies, but the explosion ends up at the scenes centre.
    how can i add the explosion at the enemy positon when it dies? and also, i want the enemy to be destroyd like 2 sec after the explosion, but i didnt get the yeld thing to work

    heres the script
    Code (CSharp):
    1.     public float hitPoints = 100f;
    2.     public float currentHitPoints;
    3.     public GameObject destroyFX;
    4.  
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         currentHitPoints = hitPoints;  
    10.     }
    11.  
    12.     void Update () {
    13.  
    14.         if(currentHitPoints <= 0)
    15.         {
    16.             currentHitPoints = 0;
    17.             Die();
    18.         }
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Die ()
    24.     {
    25.         if(gameObject.tag == "Enemy")
    26.         {
    27.             Instantiate(destroyFX);
    28.             Destroy (gameObject);
    29.         }
    30.     }
    31. }
    32.  
     
  2. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158

    You need to record the Gameobject position and then apply that in you instantiate string.
    Code (csharp):
    1.  Vector3 playerpos = gameObject.transform.position
    2.  instantiate(destroyfx, playerpos.position, playerpos.rotation);
    3.  
    To create a delay in your destroy simply add a ,2f after gameObject in your Destroy line.
    This will delay the destruction of the object.
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329

    Thanks for your reply!

    i hope i put your lines at the right places? because im getting error on position and rotation
    if i do
    Code (CSharp):
    1.  error CS1061: Type `UnityEngine.Vector3' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.Vector3' could be found (are you missing a using directive or an assembly reference?)
    Code (CSharp):
    1.     void Die ()
    2.     {
    3.         if(gameObject.tag == "Enemy")
    4.         {
    5.             Vector3 playerpos = gameObject.transform.position;
    6.             Instantiate(destroyFX, playerpos.position, playerpos.rotation);
    7.             Destroy (gameObject);
    8.             explosionFXSound.Play ();
    9.         }
    10.     }
    11. }
    12.  
     
  4. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Apologies, the instatiate line should be playerpos.transform.position
     
  5. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    That should have been:

    Code (csharp):
    1.  Transform playerTransform = gameObject.transform
    2. instantiate(destroyfx, playerTransform.position, playerTransform.rotation);
    3.  
     
  6. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    It says the same about "transform" now and same about pos and rot, do i missing something?
     
  7. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Thanks that solved it.

    However, there is something wierd here,if i have my empty cube in the scene and adding sound and explosion FX from the project, i see 1 explo at the cube and 1 in the centre of the scene and do not hear any sound.

    i can only hear the sound if i assignt all FX from the Heirarchy + that the explo is "play on awake" as it should, but you see the explo on start, and this also do so i cant self destruct the clones since they will destruct on start then
     
  8. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    You dont appear to be calling the audio source in your start ().

    So unity doesnt know where to grab your audio from.

    need to add a GetComponent<audiosource>() up in start()
     
  9. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329

    This getting me nuts, it doesnt matter what i do, i hear no sound when the object destroys.

    I assignt the health script to the cube inside the project, then i assign the sound prefab in the script also in the project.
    Then i drag it to the Heirarchy to get it to the scene where i want it.

    And also, if i trying to assign a sound prefab in the project by klicking on the popup window, it do not findany sound prefab, however..i can drag it from the project to the inspector, maybe thats the problem that it cant find it in the project?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShipHealth : MonoBehaviour {
    5.  
    6.     public float hitPoints = 100f;
    7.     public float currentHitPoints;
    8.     public GameObject destroyFX;
    9.     public AudioSource explosionFXSound;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         GetComponent<AudioSource>();
    16.         currentHitPoints = hitPoints;  
    17.     }
    18.  
    19.     void Update () {
    20.  
    21.         if(currentHitPoints <= 0)
    22.         {
    23.             currentHitPoints = 0;
    24.             Die();
    25.         }      
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Die ()
    30.     {
    31.         if(gameObject.tag == "Enemy")
    32.         {
    33.             Transform playerTransform = gameObject.transform;
    34.             Instantiate(destroyFX, playerTransform.position, playerTransform.rotation);
    35.             explosionFXSound.Play();
    36.             Destroy(this.gameObject);
    37.         }
    38.     }
    39. }
     
  10. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    I had the same issue. You are destroying the object before the sound has a chance to play. Try commenting out the destroy code to check the sound works if it does add a delay to destroy
     
  11. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    You told me to add ,2f to the testroy before, but that freaked the sound out by sounding "beeeeeeeeeeeeeeeeeeeee"
    So i cant do it that way to deley the destruction.

    EDIT: maybe i need to check if the sound is playing and then destroy?
     
  12. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    And your audio source isn't set to loop?
     
  13. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    No its not set to loop

    EDIT: i removed the destroy line, then it spamming like 20 explotions instead of just apply one, and no sound

    EDIT2: ok.. i added some codes to see what could be wrong.
    i madwe it to check if the sound is playing, if it is it destroy the game object, else to play the audion clip without to die...and it doesnt die.

    That must mean that it cant find the object with the sound FX prefab in the project?
    Code (CSharp):
    1.     void Update () {
    2.  
    3.         if(currentHitPoints <= 0)
    4.         {
    5.          
    6.             if(explosionFXSound.isPlaying){
    7.                 currentHitPoints = 0;
    8.                 Die();
    9.             }
    10.             else
    11.             {
    12.                 explosionFXSound.Play();
    13.                 //dont die
    14.  
    15.             }
    16.         }      
    17.     }
    18.    
     
    Last edited: May 13, 2015