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

Problem with OnCollisionExit and OnCollisionStay

Discussion in 'Scripting' started by Fendtastic, Jun 1, 2019.

  1. Fendtastic

    Fendtastic

    Joined:
    Aug 10, 2018
    Posts:
    9
    Hi,

    i am coding a sort of TD Map. Now i have following Problem. I click on the Matcharea to a place
    my tower has no collisions and is able to stay there. Then my tower is set on this position.
    When i take the next tower and try to set on the same place, its not possible because the towers have colliders (big enough) and i use

     OnCollisionStay
    and a

    boolean buildPossible

    so far so good. When OnCollsionStay is called the boolean is false. When i use OnCollisionExit, the boolean gets true. everything works perfect.

    BUT when i am clicking very fast inside the Matcharea, it is possible that two towers are set in each other.
    I have also the function with

    Input.MouseButtonDown
    to set more towers. But sometimes happens, when i am moving the mouse very fast, that towers set in each other and ignore the boxcolliders. Here is the code an a picture. I hope someone is understanding my problem. Here is towerScript and part of building script:


    towerScript:

    Code (CSharp):
    1. public class Tower : MonoBehaviour
    2. {
    3.     public bool buildPossible;
    4.     private TowerBuilder r_towerBuilder;
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         buildPossible = true;
    10.         r_towerBuilder = GameObject.Find("TowerBuilder").GetComponent<TowerBuilder>();
    11.     }
    12.  
    13.     public void OnCollisionEnter(Collision col3)
    14.     {
    15.         if (col3.gameObject.tag == "TowerSet" || col3.gameObject.tag == "Enemy" || col3.gameObject.tag == "Obstacle")
    16.         {
    17.            buildPossible = false;
    18.         }
    19.     }
    20.     public void OnCollisionStay(Collision col) .
    21.     {
    22.         if (col.gameObject.tag == "TowerSet" || col.gameObject.tag == "Enemy" || col.gameObject.tag == "Obstacle")
    23.         {
    24.             buildPossible = false;
    25.         }
    26.     }
    27.     public void OnCollisionExit(Collision col2)
    28.     {
    29.         if (col2.gameObject.tag == "TowerSet" && buildPossible == false || col2.gameObject.tag == "Enemy" && buildPossible == false || col2.gameObject.tag == "Obstacle" && buildPossible == false)
    30.         {
    31.             buildPossible = true;
    32.         }
    33.     }
    34. }


    and building script:


    Code (CSharp):
    1.    
    2. if (Input.GetMouseButtonDown(0) && mouseTower.GetComponent<Tower>().buildPossible == true)
    3.         {
    4.             setMouseTower(finalMouseTowerPosition);                  
    5.         }
    6. if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButton(0) && mouseTower.GetComponent<Tower>().buildPossible == true)              
    7.        {                
    8.        setMouseTower(finalMouseTowerPosition);                
    9.        }
    10.  
    11.  
    12. public void setMouseTower(Vector3 buildPosition)
    13. {
    14.  GameObject buildedTower = Instantiate(mouseTower, buildPosition, mouseTower.transform.rotation, towerParent.transform);          
    15.  
    16.   buildedTower.tag = "TowerSet";
    17. }
    18.  
    19.  
    20.  

    Here the image:


    TowerWrong.png
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,991
    maybe try setting buildPossible default value to false? (and in Awake() or in the variable already)
    also i'd guess no need to use OnCollisionStay() at all, since you already check for in and out.
     
  3. Fendtastic

    Fendtastic

    Joined:
    Aug 10, 2018
    Posts:
    9
    At first thank you for your response! I use On collision stay, because i have an instance of my towerprefab on position of my mouse cursor. when i now click to build a tower, another instance is builded on the place the mousecursor was. with OnCollisionStay, i have much besser results. Without i have the problem discribed more often.

    and there is no sense to set the build possible Variable to false in awake, because. The tower to build the setTower stays on the cursor. And its the same Instance all the time. i think its a problem with OnCollisionExit. buildPossible get true by leaving the collision and when i move the mouse very fast. it is true and my mouse is near a other tower or so (an OnCollisionEnter is too slow maybe)... i dont know xD

    Tried OnTriggerEnter, but the problem is also there. Maybe not this much, but also there. And i am forced to use Collision instead of Triggers, because of my A* Pathfinding NavMesh.

    I hope you understand me ^^.

    greetings.