Monday, August 1, 2022
HomeGame Developmentunity - Teleporting the participant triggers distant collider

unity – Teleporting the participant triggers distant collider


I’m attempting to teleport a participant that has a Rigidbody2D hooked up. The Rigidbody2D is Dynamic, has Interpolate set to Interpolate, and has Collision Detection set to Steady.

Once I teleport the participant from Level A to Level B, the distant collider (that occurs to align with the vector from A to B) is sporadically triggered though the participant collider is nowhere close to it.

enter image description here

That is how I first tried to teleport the participant (whose rework is at Level A within the diagram above):

// tried in each Replace() and FixedUpdate()
playerRigidbody.place = pointB.rework.place;
playerRigidbody.velocity = new Vector2();

The script hooked up to the distant collider is sporadically triggered once I carry out this teleport.

My idea is that while you teleport the rework of the participant, it impacts the interpolated physics to counsel the participant is shifting very quickly in that path and so it permits the participant to collide with colliders in its extrapolated path. I might assume setting the Velocity to 0 would stop that, however possibly not?

Issues I attempted to repair it:

  1. Set the participant GameObject to a special Tag quickly so colliders that filter to the “Participant” tag will not be triggered by it. Nonetheless, this solely works if I alter it with a while buffer earlier than the teleport and alter it again with a while buffer afterwards. I attempted it like this:
personal int fixedUpdateCount = -1;
personal bool doingTeleport = false;

void DoTeleport() {
    fixedUpdateCount = -1;
    doingTeleport = true;
}

void FixedUpdate() {
    if (!doingTeleport) 
        return;
    fixedUpdateCount++;
    if (fixedUpdateCount == 0) {
        participant.tag = "Participant triggers disabled";
    } else if (fixedUpdateCount == 2) {
        playerRigidbody.place = pointB.rework.place;
        playerRigidbody.velocity = new Vector2();
    } else if (fixedUpdateCount >= 4) {
        participant.tag = "Participant"
        doingTeleport = false;
    }
}

The issue is, this does not work until I enhance these buffers of two mounted updates on both aspect. I have not figured precisely how excessive it must be. This in fact makes the teleport take extra time than must be obligatory, and it feels actually hacky. If I tweak physics settings later, I am afraid it’d change the required mounted replace buffer on both aspect of the transition. It additionally is perhaps affected by efficiency hiccups.

  1. I attempted altering CollisionDetection to None in the course of the transition. This did not work until I did the buffering like above, however then it has the identical points plus the participant begins falling into the ground.

  2. I attempted altering Simulated to false earlier than doing the teleport. This merely did not work. I am unsure if it is as a result of I reenable it earlier than ready for a set replace, and so it behaves as if it was by no means disabled. Or possibly while you reenable Simulated, the earlier state of the rigidbody is sustained as if it have been solely paused relatively than reset.

playerRigidbody.simulated = false;
playerRigidbody.place = pointB.rework.place;
playerRigidbody.velocity = new Vector2();
playerRigidbody.simulated = true;
  1. What I would strive subsequent is making the Participant right into a Prefab. After which to teleport it, I merely destroy it and recreate it on the new place. This may work if the participant is moved to the brand new place earlier than its Rigidbody is hooked up. However will probably be an actual ache if the participant begins having varied stateful behaviors hooked up as I get additional into growth. I might possibly should detach these and reattach to the brand new participant. Unsure.

It seems like there must be a definitive, clear approach to teleport a Rigidbody2D to a brand new place and reset its habits with out danger of triggering different stuff. Any concepts?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments