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;
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?
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.
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;
}
}
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.
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.