Search Unity

Collision Detection too slow.

Discussion in 'Physics' started by Fendtastic, Jun 7, 2019.

  1. Fendtastic

    Fendtastic

    Joined:
    Aug 10, 2018
    Posts:
    9
    Hi everyone,

    i'am pretty new here, but i hope to get help.

    I try to describe my problem shortly.

    i have two buildings on a plane(lets say GroundBuildings) . And one object on the mouseposition(MouseBuilding).
    all the Buildings have colliders. With a mouseclick i can release the MouseBuilding. This is not possible when the collider of the MouseBuilding enters the colliders of the GroundBuildings. Thats what i want and it works well. I work here with a normal boolean variable.

    And now my problem. When the building on my mouseposition (MouseBuilding) leaves one of the GroundBuildings the boolean gets true. But when i now enter the second GroundBuilding , there is a short moment the boolean stays true even though the colliders hit each other and with fast mouse click i can release the MouseBuilding in the second Groundbuilding.

    I think the problem is, the Collisiondetection is to slow. I use OnCollisionEnter and OnCollisionExit.

    I tried to use OnCollisionStay to remain the boolean false. But same problem. And i have the same problem with OnTriggerEnter and OnTriggerExit.

    Also tried the collisiondetection Continous and Continous Dynamic etc. Problem is sill there.

    Would be awesome if someone can help me.
     
  2. Wip3ou7

    Wip3ou7

    Joined:
    Mar 9, 2015
    Posts:
    41
    *edit: you can ignore this, better solution in next reply*

    Check for collision manually in Update() not FixedUpdate().
    Then every frame that is rendered you will be checking for overlap, instead of at the regular interval of the phsyics engine.

    You can also change the fixedUpdate frequency under the Time settings (menu: Edit > Project Settings, then the Time_ category)... "lets you set a number of properties that control timing within your game."

    Fixed Timestep: A framerate-independent interval that dictates when physics calculations and FixedUpdate() events are performed.

    You could also limit the framerate of the game to match the physics step.
     
    Last edited: Jun 12, 2019
  3. Wip3ou7

    Wip3ou7

    Joined:
    Mar 9, 2015
    Posts:
    41
    Actually I just figured out an even better solution. You can wait until the next physics step to check if the building is allowed to be placed and then perform the placement on that frame. Thats probably the simplest solution. Do your checks in FixedUpdate().

    So, in a normal update the user clicks a spot to place a building and you cache that location.
    Then, the next fixedUpdate comes and now you check for overlap in that space.
    If no, then you place the building at the location the user clicked.
    Else, you reject.
     
    Last edited: Jun 12, 2019
  4. Wip3ou7

    Wip3ou7

    Joined:
    Mar 9, 2015
    Posts:
    41
    Alternatively, in FixedUpdate() cache the position of the MouseBuilding and wether it was placeable or not.
    When the user clicks in Update() you use the last check made in FixedUpdate() and place at that position if the check was valid.

    I think this is the best approach.
     
  5. Fendtastic

    Fendtastic

    Joined:
    Aug 10, 2018
    Posts:
    9
    Thank you so much :) You helped me a lot.
     
    Wip3ou7 likes this.