Search Unity

Teleport a CharacterController AND trigger OnTriggerExit.

Discussion in 'Physics' started by FOXAcemond, Jun 11, 2020.

Thread Status:
Not open for further replies.
  1. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Hi,

    I'm struggling to find a proper way to teleport a GameObject with a CharacterController attached from point A to point B and make sure that OnTriggerExit and OnTriggerEnter is fired properly.

    If I do:
    Code (CSharp):
    1.  
    2. characterController.enabled = false;
    3. transform.position = target.position;
    4. characterController.enabled = true;
    5.  
    If the object teleports inside a trigger zone or teleports from a trigger zone OnTriggerExit and OnTriggerEnter are not fired.

    And if I do:
    Code (CSharp):
    1.  
    2. characterController.transform.position = target.position;
    3.  
    Sometimes it works, sometimes it doesn't, depending on what the CharacterController has to say.

    Using OnTriggerStay instead in my collider is HELL to combine with its logic.

    I'm considering replacing my CharacterController with a RigidBody and some code.
    Isn't there a proper way to do teleportation with a CharacterController?
     
    HenryTownshend likes this.
  2. HenryTownshend

    HenryTownshend

    Joined:
    Jun 19, 2017
    Posts:
    14
    I am having the exact same problem. I dunno what goes on with Triggers and using a CharacterController, but it has jus caused me headaches. I also thought of rewriting my CharacterControls to use rigidbody and Physics because of this. Not even shure if it would help, though. Somehow OnTriggerExit does not get called when you simply teleport stuff out of it. It seems that some movement needs to happen per frame to detect something going out of the trigger. That would be the only explanation I could think of as of now.

    I am also using "charController.transform.position" but for me it seems to have not affected the problem.

    I then tried to move the position outside of the current trigger and then move it to the target trigger with no luck. My method looks like this:

    Code (CSharp):
    1.   void SpawnPlayerInNewScene()
    2.     {
    3.         Debug.Log("SpawningPlayerToNewScene");
    4.         charController.enabled = false;
    5.         charController.transform.position = Vector3.zero;
    6.         charController.transform.position = nextTrigger.position;
    7.         charController.enabled = true;
    8.     }
    Would be a bummer if this is not possible. It seemed so trivial at first to do this teleportation stuff. Now I am getting out of ideas. I bet it needs to happen over multiple frames, but I have no idea how you could stretch that. Maybe with a Coroutine yield return? But that would seem very ugly and probably unnecessary.

    Also, using "OnTriggerStay" did not work for me as well. It seems, as it is based on FixedUpdate, you can't fire it just once, no matter what you do. It always got called at least 2 times for me. I also read it's unreliable to handle any input inside of it. So best practice is, as i've read, to use a flag to turn to true in "OntriggerEnter" and turn it off in "OnTriggerExit" and then do whatever you need to do in regular Update. But as none of these get fired in this situation, this isn't very helpful.
     
  3. HenryTownshend

    HenryTownshend

    Joined:
    Jun 19, 2017
    Posts:
    14
    Meanwhile I found this post:

    https://answers.unity.com/questions/176712/alternate-for-ontriggerexit.html

    It seems that Physics.Overlap would be a good alternative in our case. Cause as I suspected, Triggers seem to only register things actually moving in and out, not teleporting.

    Another alternative I started is to use is RayCasting for the job of proximity detection. You could make your ray very short and if it hits the teleporter you could call your teleporting functuinality. Before that, I've used a mix of Trigger logic, input and raycsting as conditions, that was causing me trouble. With raycasting and the input query alone it works to have my charController teleported. It's a good learning to know that triggers are only usable with entities that actually move in and out. Any instant translation on it's own that is not noticed by the physics system will simply be ignored as it seems. I guess this would be no different when using a Physics based charController opposed to the charController component.
     
    Last edited: Jun 13, 2020
  4. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I ended up implementing a custom RigidBody controller. Turned out much easier than expected, got me rid of this bug and gives me more control about character movement. And with this method, triggers register teleportations performed with transform.position.
     
  5. zirker09

    zirker09

    Joined:
    Apr 2, 2013
    Posts:
    2
    I had a similar issue and fixed it by adding a Capsule Collider onto the same game object as my Character Controller and the OnTriggerExit method started working as expected.
     
  6. GloriaVictis

    GloriaVictis

    Joined:
    Sep 1, 2016
    Posts:
    133
    Bumping the topic - it is the biggest flaw we are hitting right now after changing from 2017.4 into 2019.4 - CharacterController detection of OnTriggerEnter/Exit while teleporting player out are totally inconsistent.

    We have tried playing with autosync, syncing after setting transform, setting transform - autosync and doing a very small move doesn't help that either.

    Adding Capsule Collider didn't fixed our case, it is so inconsistent that we cannot find 100% working reproduction, it is like not working on half of the cases.
     
    Last edited: Sep 11, 2020
    dzhurabaiev likes this.
  7. dzhurabaiev

    dzhurabaiev

    Joined:
    Sep 19, 2019
    Posts:
    1
    I've stucked with similar problem. Trigger Enter/Exit doesn't work properly and it's absolutely disgusting to see that devs do nothing. Devs, if you read it, pls give feedback or say what to do in this case. Thanks
     
  8. Yerendi

    Yerendi

    Joined:
    Mar 11, 2016
    Posts:
    18
    I ran into the same problem, it looks like there are some cases when Collider simply does not invoke OnTriggerEnter/Exit, but I can't find exact repro - you can run same scene two times getting two different results.
     
  9. FiveFingerStudios

    FiveFingerStudios

    Joined:
    Apr 22, 2016
    Posts:
    510
    I'm running into this issue way into 2022. I was using a rigidbody and capsule collider before and had not issue. I recently switched to a character controller and started having this issue. Is there a fix for this?
     
    Rouddem and HunterAhlquist like this.
  10. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    132
    Facing this same issue right now too.
     
  11. LandePlage

    LandePlage

    Joined:
    May 21, 2016
    Posts:
    33
    I'm experiencing this too, between these two objects:
    - Camera: RigidBody2D (kinematic), CircleCollider2D.
    - Zone: BoxCollider2D (trigger enabled).

    Zone is the object that is checking OnTriggerEnter and OnTriggerExit.

    When the camera moves slowly out and in of the zone, it works. When I teleport out, it does not.
    Manual states that you shouldn't use Transform.position when using a rigidbody, but use rigidbody.MovePosition() instead - but that doesn't seem to fix it.

    Version 2022.3.7f1

    Edit: Actually, I realized after making this post that it was working all along - my method of checking it was flawed. I was using it for audio, and had the same audio event hooked up to both the zone I was leaving and then one I was entering.
     
    Last edited: Sep 10, 2023
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    In the future, please create your own thread rather than necroing an existing one. The 2D physics and 3D physics are completely different systems so a problem on one will not be "the same problem" on the other.

    Thanks.
     
Thread Status:
Not open for further replies.