Teleport Not Working all the time

Hello,

I’m using IAstarAI and I’ve got some issue with the Teleport function. It seems to be very hit & miss, sometimes it works fine and other times it doesn’t.

Setup:
I’ve got my AI being pooled at the start of the game & are initialized at Vector3.zero & then disabled.

I then spawn them where I want, later on, to do this I spawn them from the pool & then use Teleport to move them. This generally works, but for some reason, some AI will just no teleport, even though when i debug log them, the location being sent to Teleport is correct.

When they are spawned from the pool I’m setting the values as seen below;

        this.agent.canMove = true;
        this.agent.canSearch = true;
        this.agent.isStopped = true;

Then the teleport code is:

        this.agent.canSearch = true;
        this.Destination = origin;
        this.agent.destination = origin;
        this.agent.Teleport(origin);   

If I spawn 100 AI, with a small delay in between spawning, most of them will spawn fine, but there is always some who don’t teleport…

Should I be setting some other value just before teleporting them?

Thanks for any help!

Hi

Which movement script are you using?
Could you log for example
Debug.Log(this.agent.position + " " + origin);
right after you try to teleport the agent?

I’m using the AIPath script for movement.

When I use that debug.log both values are the same, but the AI’s position clearly isn’t correct.

Exact code:
public void Spawn(Vector3 origin, Quaternion rotation)
{
this.MovementSpeed = Random.Range(this.minMoveSpeed, this.maxMoveSpeed);
this.agent.canSearch = true;
this.Destination = origin;
this.agent.destination = origin;
Teleport(origin);
Debug.Log(this.agent.position + " | " + origin);
this.transform.rotation = rotation;
}

DebugOutput with the input of (16.2, 4.2, 13.1)

  • (16.2, 4.2, 13.1) | (16.2, 4.2, 13.1)

After more debugging, it appears to happen on the frame after they teleport. I have no other functions being used to move it after it teleports.

Hi

Try to disable all other components on the agent and see if it still happens. I’ve been looking through the code, and I cannot find anything that would cause this.

Hi,

So made a simple test movement script & have the same results. Below is my exact code & I attached it to an object in a new, empty scene and have no other script attached. When pressing the teleport key the NPC would sometimes teleport with no issues, but most of the time it would teleport on 1 frame & then go back to its old position on the next frame.

I can send a GIF/Video if you would like.

Scripts Attached:

  • Seeker
  • AIPath
  • CharacterController
  • My Test Script, see below.

Code:
public bool canMove = true;
public Transform targetPosition;
public Transform teleportPos;
private IAstarAI agent;

public void Start()
{
    agent = GetComponent<IAstarAI>();
}

public void Teleport()
{
    Debug.Log("Teleport");
    canMove = false;
    agent.destination = this.teleportPos.position;
    agent.Teleport(this.teleportPos.position);
}
public void Update()
{
    if (Input.GetKeyDown(KeyCode.U))
    {
        Teleport();
    }

    if (canMove)
    {
        agent.destination = this.targetPosition.position;
    }
}

Hi

I downloaded the script and tested it, but unfortunately I cannot replicate the behavior. For me, it works perfectly every time :confused:

After some more testing, it’s the CharacterController causing the issue.

If I remove it and try it, I get no issues.

1 Like

I have the exact same issue as @Aloox (with 4.2.18 and Unity 2023.1.19f1). Teleport does NOT work for me using the same method above - the agent just walks from A to B. Removing the CharacterController also fixes the issue.

@aron_granberg can you please try this again and if it works, please let us know what you did. Thanks.

EDIT: I’m using the Pro version if that makes any difference.

UPDATE: Removing the CharacterController fixes the teleport issue but my agent no longer navigates properly on two layered grid graphs. The agent’s Seeker has one traversable graph but ends up walking on other graphs when there is no CharacterController.

So, with CharacterController - navigation = success, teleport = failure;
without CharacterController - navigation = failure, teleport = success. :frowning:

UPDATE 2: If I disable the CharacterController, then teleport, then re-enable the CharacterController it does teleport correctly. I tried playing around with the Layer Overrides on the CharacterController but made no difference.

Oh thank god, I was afraid I was the only one with this problem.

Just like the others, the problem is with the CharacterController. If I disable it or replace it with a regular CapsuleCollider, my object teleports normally. An interesting thing, though, is that this problem still occurs for me even if I replace .Teleport(position) with a simple transform.position = position. I use AstarPath.active.GetNearest() to find a valid position on the graph to teleport to, but it does seem to be more of a problem with CharacterController rather than A*, for me at least.

Edit: Here’s a Unity forum post talking about the problem: https://forum.unity.com/threads/cant-teleport-player-with-charactercontroller-and-rigidbody.728402/

Because AIBase knows the CharacterController, I think it should also handle this in Teleport().

Anyway, like the others said, this can be solved like this:

CharacterController.enabled = false;
IAstarAI.Teleport(position);
CharacterController.enabled = true;

Hi

I haven’t been able to replicate this, even with a CharacterController. Are there any particular physics settings that you use to trigger it?

Okay… I’ve tested both the latest and the old version, and it’s probably only happening on old versions.

It’s reproducible in this environment:

  • Unity 2021.3.33.
  • Path-finder 4.2.19.
  • In Example4_Recast_Navmesh2, replace TargetMover.cs:67 with Teleport().
if (positionFound && newPosition != target.position) {
    //target.position = newPosition; 
    ais[0].Teleport(newPosition);

    if (onlyOnDoubleClick) {
        ...
1 Like