Forum rules - please read before posting.

Character doesn't move when using player switching

edited October 2020 in Technical Q&A

HI,

Background:

I'm using a custom character that listens for changes to the target position (AC) then moves the character using NavMeshAgent commands

Problem:

When I place my character directly in the scene it moves around fine, however, as soon as I enable character switching and assign my prefab as the only and default character, it doesn't move. The character is spawned at the player start fine, it animates the idle animation etc, it just doesn't move.

I have done some debugging and it seems that the NavMeshAgent.destination property is being set a default position and the path is being cleared every frame.

For example, the player start is positioned at -2, 0, 2 which is returned as the first 'target position', then I click somewhere and the target position changes (e.g. 5,0,10), this is then assigned to the NavMeshAgent as normal. Then on the next frame, the destination is set back to what it was at the start and the path is removed, no path pending, no has path.

I have only a single place where the path is assigned to the character, I have no other scripts in the world that execute. I have tried adding an OnStart action that switches to that player just in case it wasn't the default.

Any help would be gratefully received.

Comments

  • edited October 2020

    Right, I've found the issue.

    As mentioned above my player start is at (-2, 0, 2) which is exactly where my is character spawned, good stuff. At (0, 0, 0) I had a piece of geometry that was walkable (NavMesh) on the top but not connected to the main ground mesh.

    The NavMeshAgent was stuck on top of this geometry separated from my character, and because it was disconnected from my main navigation mesh on the ground there was no way for it to get down.

    I removed that piece of geometry and voila the character moves again and the navmesh agent is in line with the player character.

    I don't think this is the desired functionality though, the chances are that another geometry is going to be at (0,0,0) at some point.

    I noticed that when using player switching the character is set to 'DontDestroyOnLoad' meaning I won't get the Start/Awake events. Are there any events I can listen to in the character component that will fire when the scene is loaded and the player has been positioned etc? I can use this to forcibly align the NavMeshAgent with the character.

    Edit: At the best guess, I think that the player character is being spawned into the scene at (0,0,0) then moved to the player start position, but at the point of spawning the navmesh agent is stuck at the original spawn location.

  • For reference, this topic is clearly explained in the manual (RTFM).

    Section 3.3.3 - Custom Motion Controllers

    Specifically, you need to listen for the OnTeleport and OnSnapRotate events and call the warp method of the navmesh agent to correctly position the agent when AC positions your character.

  • Are there any events I can listen to in the character component that will fire when the scene is loaded

    OnInitialiseScene is called once the scene is been initialised (but before any saved data is loaded in). The OnAfterChangeScene event is called once saved data has been loaded.

    More appropriate here, though, is likely the OnPlayerSpawn event, which is called once a Player has been spawned via the Settings Manager.

  • @ChrisIceBox

    Brilliant, thank you, Chris.

    I feel quite silly, all of this is explained in the manual.

  • No worries, it's a big Manual!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.