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

[SOLVED] Don't instantiate particle when on same platform object

Discussion in 'Scripting' started by Jojoba007, Jun 13, 2018.

  1. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    Hi. The following. I have some platforms in my game. If the player jumps from a platform and hits the side of the next platform (a missed jump) it instantiates some dust particles because the player hits the side of that platform.

    Now the problem: If the player falls from the platform he is standing on and enters that same specific trigger on the side of the platform it will also instantiate those dust particles. This is not what I want. I only want those particles to instantiate if the player comes from another platform. How should I approach this? Can someone point me in the correct direction?

    The platform is a main object with a trigger (top trigger) and 1 child objects with a trigger. See example screenshot: http://prntscr.com/jugubz

    Regards
     
  2. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    Maybe you could store the last platform the player was on, and check if the trigger zone you hit is corresponding to that platform or the next.
     
    Jojoba007 likes this.
  3. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    Thanks for your reply. Could you maybe point me in the right correction for an example. I'm still quit new to coding.
     
  4. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    Take a look at this, it might help :
    Code (CSharp):
    1. public class Example : MonoBehaviour {
    2.  
    3.     Transform lastPlatform;
    4.  
    5.     Transform GetPlatformFromSide(Transform side)
    6.     {
    7.         // Return the platform corresponding to input side
    8.     }
    9.  
    10.     //I assume you have something similar to this
    11.     void OnCollisionEnter(Collision collision)
    12.     {
    13.         if (collision.collider.CompareTag("platformTrigger"))
    14.         {
    15.             lastPlatform = collision.collider.transform;
    16.         }
    17.  
    18.         if (collision.collider.CompareTag("sideTrigger"))
    19.         {
    20.             Transform sidePlatform = GetPlatformFromSide(collision.collider.transform);
    21.  
    22.             if ( lastPlatform != sidePlatform)
    23.             {              
    24.                 //particles instantiation code
    25.             }
    26.         }
    27.     }
    28. }
     
    Jojoba007 likes this.
  5. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148

    Thanks for your time and help so far. I understand it partly. I'm still not sure how to get the platform side. This is the code I have on the child platform object for now (which has the tag "platformSideTrigger"):


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HitPillar : MonoBehaviour {
    6.  
    7.     //Setting the ParticleSystem
    8.     public GameObject particletHitPillar;
    9.     private GameObject hitPillarObject;
    10.     private ParticleSystem hitPillarParticle;
    11.  
    12.     //Getting script from Pillar
    13.     public GameObject dirtPillarObject;
    14.     private PillarDidTrigger pillarDidTriggerScript;
    15.  
    16.  
    17.     //check if player is on same platform then the particles for side hit should not be instantiated
    18.     Transform lastPlatform;
    19.  
    20.     //Collider
    21.     Collider otherCol;
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.    
    27.         // Getting script from Pillar
    28.        pillarDidTriggerScript = dirtPillarObject.GetComponent<PillarDidTrigger>();
    29.  
    30.        otherCol.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
    31.     }
    32.  
    33.  
    34.  
    35.     public void OnTriggerEnter(Collider otherCol)
    36.     {
    37.  
    38.  
    39.  
    40.  
    41.  
    42.         if (otherCol.CompareTag("Platform"))
    43.         {
    44.             lastPlatform = otherCol.transform;
    45.         }
    46.  
    47.         if (otherCol.tag == "Player" && otherCol.CompareTag("platformSideTrigger"))
    48.  
    49.  
    50.         {
    51.  
    52.             Transform sidePlatform = GetPlatformFromSide(otherCol.transform);
    53.  
    54.             if (lastPlatform != sidePlatform)
    55.  
    56.             {
    57.  
    58.                 //Instantiate the dust particles when hitting the side of the platform
    59.                 hitPillarObject = Instantiate(particletHitPillar, otherCol.transform.position, otherCol.transform.rotation) as GameObject;
    60.                 hitPillarParticle = hitPillarObject.GetComponent<ParticleSystem>();
    61.                 //hitPillarParticle.transform.Rotate(-90, 0, 0);
    62.             }
    63.         }
    64.  
    65.  
    66.     }
    67.  
    68.     Transform GetPlatformFromSide(Transform side) {
    69.  
    70.         // Some code??
    71.  
    72.         return side;
    73.     }
    74.  
    75. }
     
    Last edited: Jun 14, 2018
  6. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    For the GetPlatform function, i would setup something like this for the pillar in the scene : https://i.imgur.com/PqYxKvs.png

    And use this line of code inside :

    Code (CSharp):
    1.  Transform GetPlatformFromSide(Transform side) {
    2.    
    3.         return  side.parent.GetChild(1);
    4.  
    5.     }
    Just make sure to always put the topTrigger child in the 2nd position in the hierarchy, it won't work otherwise.
     
    Jojoba007 likes this.
  7. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148
    Thanks again for your reply. It's not working yet. And I'm also getting the following warning on line 30:

    NullReferenceException: Object reference not set to an instance of an object
    HitPillar.Start () (at Assets/Scripts/HitPillar.cs:30)

    This is the code on line 30:
    Code (CSharp):
    1.        otherCol.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
    - I have the main parent platform with the topTrigger
    - The first child of the platform is the sideTrigger which contains the script
    - and the 2nd child is the collider which isn't a trigger.

    This is the code so far. I'm still not getting it completely ;-)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HitPillar : MonoBehaviour {
    6.  
    7.     //Setting the ParticleSystem
    8.     public GameObject particletHitPillar;
    9.     private GameObject hitPillarObject;
    10.     private ParticleSystem hitPillarParticle;
    11.  
    12.     //Getting script from Pillar
    13.     public GameObject dirtPillarObject;
    14.     private PillarDidTrigger pillarDidTriggerScript;
    15.  
    16.  
    17.     //check if player is on same platform then the particles for side hit should not be instantiated
    18.     Transform lastPlatform;
    19.  
    20.     //Get collider
    21.     Collider otherCol;
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.    
    27.         // Getting script from Pillar
    28.        pillarDidTriggerScript = dirtPillarObject.GetComponent<PillarDidTrigger>();
    29.  
    30.        otherCol.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
    31.     }
    32.  
    33.  
    34.  
    35.     public void OnTriggerEnter(Collider otherCol)
    36.     {
    37.  
    38.  
    39.  
    40.  
    41.  
    42.         if (otherCol.CompareTag("topTrigger"))
    43.         {
    44.             lastPlatform = otherCol.transform;
    45.         }
    46.  
    47.         if (otherCol.tag == "Player" && otherCol.CompareTag("sideTrigger"))
    48.  
    49.  
    50.         {
    51.  
    52.             Transform sidePlatform = GetPlatformFromSide(otherCol.transform);
    53.  
    54.             if (lastPlatform != sidePlatform)
    55.  
    56.             {
    57.  
    58.                 //Instantiate the dust particles when hitting side of platform
    59.                 hitPillarObject = Instantiate(particletHitPillar, otherCol.transform.position, otherCol.transform.rotation) as GameObject;
    60.                 hitPillarParticle = hitPillarObject.GetComponent<ParticleSystem>();
    61.                 //hitPillarParticle.transform.Rotate(-90, 0, 0);
    62.             }
    63.         }
    64.  
    65.  
    66.     }
    67.  
    68.     Transform GetPlatformFromSide(Transform side) {
    69.  
    70.         return side.parent.GetChild(1);
    71.     }
    72.  
    73. }
    74.  
     
    Last edited: Jun 16, 2018
  8. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    So, i initially thought this script were on the player, after re-reading more in depth i think this should work now :

    So this should go on the player :
    Code (CSharp):
    1.  public Transform lastPlatform;
    2.  
    3.     void OnTriggerEnter(Collider otherCol)
    4.     {
    5.         // if player hit the top platform, store it in lastplatform
    6.         if (otherCol.CompareTag("topTrigger"))
    7.         {
    8.             lastPlatform = otherCol.transform;
    9.         }  
    10.     }

    And this on your sidetrigger :

    Code (CSharp):
    1.     //Setting the ParticleSystem
    2.     public GameObject particletHitPillar;
    3.     private GameObject hitPillarObject;
    4.     private ParticleSystem hitPillarParticle;
    5.  
    6.     //Getting script from Pillar
    7.     public GameObject dirtPillarObject;
    8.     private PillarDidTrigger pillarDidTriggerScript;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         // Getting script from Pillar
    14.         pillarDidTriggerScript = dirtPillarObject.GetComponent<PillarDidTrigger>();  
    15.     }
    16.  
    17.     void OnTriggerEnter(Collider otherCol)
    18.     {
    19.         //is the player colliding with me ?
    20.         if (otherCol.CompareTag("Player"))
    21.         {
    22.             // then get the platform above me
    23.             Transform topPlatform = GetPlatformFromSide(transform);
    24.  
    25.             //get player script to access his last platform
    26.             Transform lastTopPlatform = otherCol.GetComponent<Player>().lastPlatform;
    27.  
    28.             // is the player last top platform the platform above me ?
    29.             if (lastTopPlatform != topPlatform)
    30.             {
    31.                 //Instantiate the dust particles when hitting side of platform
    32.              
    33.                 //i assume you want to instantiate the particle where the player hit;
    34.  
    35.                 // this get our collider and return the position on it's outside shell closest to the player
    36.                 Vector3 hitPoint = GetComponent<Collider>().ClosestPointOnBounds(otherCol.transform.position);
    37.  
    38.                 //then spawn the effect at that point, also, rotation when not relevent should be Quaternion.identity
    39.                 hitPillarObject = Instantiate(particletHitPillar, hitPoint, Quaternion.identity) as GameObject;    
    40.                  
    41.                 hitPillarParticle = hitPillarObject.GetComponent<ParticleSystem>();
    42.                 //hitPillarParticle.transform.Rotate(-90, 0, 0);            
    43.             }
    44.         }
    45.     }
    46.  
    47.     Transform GetPlatformFromSide(Transform side)
    48.     {
    49.         return side.parent.GetChild(1);
    50.     }
    51.  
    Does it makes more sense now ? :)
     
    Last edited: Jun 17, 2018
    Jojoba007 likes this.
  9. Jojoba007

    Jojoba007

    Joined:
    Feb 11, 2017
    Posts:
    148

    It makes completely sense now. Got it working and learned a lot from it. Thank you so much for taking your time to let me better understand this. Works like a charm. ;)
     
  10. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    I'm glad you figured it out, happy coding !
     
    Jojoba007 likes this.