Cannot set Position/Rotation of NPCs Directly in Code.

pako

New member
Hi,

I'm using Behavior Designer 1.7.3 and Unity 2021.3.6f1 on Windows 10.

I have several NPCs with just the Seek Task from the Movement pack. They all seek the Player. After one of them "catches" the Player, or if the Player completes his mission, I want to reset the position & rotation, of all the NPCs to their spawn points, using their transforms. This has proved impossible to achieve, even after Disabling the BT, setting navMeshAgent.isStopped = true, navMeshAgent.updatePosition = false and navMeshAgent.updateRotation = false (among other things that I tried). Nothing works! e.g. I set the NPC transform.position = spawnPoint.position, but the NPC appears anywhere else, except its spawnPoint.position.

I'd really appreciate your help to solve this!
 
Last edited:
If the agent isn't moving locations then chances are the task is still running and setting the position every frame. Disabling the behavior tree will definitely stop the task from running, maybe something else is setting the transform?

If you are using our character controller make sure you set the position using SetPosition on the CharacterLocomotion and not the transform.
 
Hi Justin,

Thank you for your reply.

Disabling the behavior tree works only temporarily. More specifically, the position & rotation, of all the NPCs do get set correctly (for a moment). However, after reenabling the BT, their positions & rotations, get set immediately to somewhere near where they were before setting their positions/rotations to their spawn points.

So, visually, after the user clicks on the "continue level" button, all the NPSs are "teleported" to other positions, far away from their spawn points, and they start their Seek tasks from there.

I enabled the BT "Restart When Complete" and "Reset values on Restart" but they don't seem to have an effect.

I've double-checked that nothing else set the NPC transform, and I'm not using your character controller for this one.
 
It sounds like something else is setting the transform values. I recommend trying to isolate it within a small scene to see if you can pinpoint what is causing it.

The movement pack tasks do not actully set the position/rotation, only set the new transform values and then it's up to the underlying pathfinding implementation to do the actual movement.
 
Actually, the scene is quite small anyway.

Also, all NPCs have the Enemy.cs script on them, and Enemy.cs isn't referenced from any other script. Moreover, there's no call to transform.position & transform.rotation anywhere else inside the Enemy script, besides the single point where it sets the NPCs back to their Spawn Points.

A little side note, on how the problem/solution developed to this point:
In my previous attempts, if I didn't control, somehow the NavMeshAgent, there were all kinds of weird positioning of the NPCs after disabling the BT. So, after lots of experimenting, the only way I could make it work was to set agent.isStopped = true, after disabling the BT, and then agent.isStopped = false after enabling the BT. Similarly for agent.updatePosition =false/true, and agent.updateRotation = false/true. Subsequently, I would set the NPCs' Seek task target to their spawn points, because even with these settings it was impossible for the NPCs to remain in their spawn points after I set it in the script.

Well, that worked, not as I wanted it to, but at least it worked! However, gameplay testing showed that this solution had some problems in some situations, and I had to find the proper solution, hence my post.

Back to today... I commented out all references to the NavMeshAgent inside Enemy.cs and, set breakpoints on every possible method call, and started stepping through code, inspecting values, etc.

Well, after setting the NPCs transform.position & transform.rotation back to their Spawn Points in script, the NPCs in the scene appeared anywhere else, but their spawn points (as expected, actually).

Therefore, since the BT gets disabled properly when it should, the only other culprit could be the NavMeshAgent itself.

So, I thought I might just try to disable the NavMeshAgent component after disabling the BT, and then reenable it after the BT gets reenabled. And finally, it worked!!! Why didn't I think of this before, instead of messing about with setting agent.isStopped? it would have saved a lot of time and headaches!

Anyway, I wonder why I have to do that manually, instead of relying on the BT to do it automatically, after the BT gets enabled/ disabled.
 
Top