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

How do i detect for no collision before update

Discussion in 'Scripting' started by Epic-Username, Aug 30, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    In this script i have created i want the the gameobject to detect if a player or enemy collides with it, then if it does don't create the block, but destroy itself, but the update gets called before the collision how can i change the script so it works how i want it to?
    Code (CSharp):
    1. public bool OCollision = false;
    2.  
    3.     public GameObject Block_object;
    4.  
    5. void OnTriggerEnter(Collider other){
    6.         if (other.gameObject.tag == "Player" || other.gameObject.tag == "Enemy" || other.gameObject.tag == "Block") {
    7.             OCollision = true;
    8.         }
    9.     }
    10.     void Update(){
    11.         if (OCollision == false) {
    12.             Destroy (this.gameObject);
    13.             Debug.Log (OCollision);
    14.             Instantiate (Block_object, transform.position, transform.rotation);
    15.         } else if (OCollision == true) {
    16.             Destroy (this.gameObject);
    17.         }
    18.  
    19.     }
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    You could try adding another boolean variable into the OnTriggerEnter function that is set to true whenever a collider enters it. Then, add one more condition to your code in the Update() function that checks to see if that variable is true or not -- then run your other conditions within that.

    Maybe something like this:

    Code (CSharp):
    1. public bool OCollision = false;
    2.  
    3.     public GameObject Block_object;
    4.     public bool didSomethingCollide = false;
    5.  
    6. void OnTriggerEnter(Collider other){
    7.      
    8.         didSomethingCollide = true;
    9.  
    10.         if (other.gameObject.tag == "Player" || other.gameObject.tag == "Enemy" || other.gameObject.tag == "Block") {
    11.             OCollision = true;
    12.         }
    13.     }
    14.     void Update(){
    15.         if(didSomethingCollide == true) {
    16.              if (OCollision == false) {
    17.                  Destroy (this.gameObject);
    18.                  Debug.Log (OCollision);
    19.                  Instantiate (Block_object, transform.position, transform.rotation);
    20.              } else if (OCollision == true) {
    21.                  Destroy (this.gameObject);
    22.              }
    23.  
    24.              didSomethingCollide = false;
    25.         }
    26.  
    27.     }
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    What i want it to do is if it has not collided with anything(which there is no method for):
    1. Destroy (this.gameObject);
    2. Debug.Log (OCollision);
    3. Instantiate (Block_object, transform.position, transform.rotation);

    4. but if it has collided with something do:

    5. Destroy(this.gameObject);
     
  4. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Does anyone know to solution to solving my problem of testing for no collision before an update?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is no direct way to do this. If you are using a primitive then you can use a sphere cast or a sweep. If you have a more complex collider then the best I've seen is to simply wait until after the next FixedUpdate and check if OnTriggerEnter fired.