Post by magique on Sept 10, 2018 1:28:40 GMT
I've written some code to allow the player to press a key and have the current player controlled object become a companion AI and to have a companion AI become the player. I actually have 2 companions to make up a party of 3 characters. However, something isn't quite working and I'm wondering if I could get some advice.
Everything works except once the AI is switched to the player it won't actually move. It acts like something is blocking it from moving. It animates, jumps, rotates, etc. But it just won't go forward. My switching algorithm consists of a script attached to each player/companion object and some auxiliary code to perform the switches. Here is the main script:
And here is the code to call those functions:
I am guessing there is some code somewhere that needs to be reinitialized or something like that, but I just haven't been able to find it.
Everything works except once the AI is switched to the player it won't actually move. It acts like something is blocking it from moving. It animates, jumps, rotates, etc. But it just won't go forward. My switching algorithm consists of a script attached to each player/companion object and some auxiliary code to perform the switches. Here is the main script:
public class AIToPlayer : MonoBehaviour
{
public vMeleeCombatInput meleeCombatInput;
public vThirdPersonController thirdPersonController;
public UnityEngine.AI.NavMeshAgent navMeshAgent;
public v_AICompanion aiCompanion;
public GameObject aiSphereSensor;
public void SwitchToPlayer()
{
aiCompanion.StopAllCoroutines();
aiCompanion.enabled = false;
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
gameObject.tag = "Player";
gameObject.layer = LayerMask.NameToLayer("Player");
thirdPersonController.enabled = true;
meleeCombatInput.enabled = true;
aiSphereSensor.SetActive(false);
} // SwitchToPlayer()
public void SwitchToAI()
{
meleeCombatInput.StopAllCoroutines();
meleeCombatInput.enabled = false;
thirdPersonController.StopAllCoroutines();
thirdPersonController.enabled = false;
gameObject.tag = "CompanionAI";
gameObject.layer = LayerMask.NameToLayer("CompanionAI");
navMeshAgent.enabled = true;
aiCompanion.enabled = true;
aiSphereSensor.SetActive(true);
} // SwitchToAI()
} // class AIToPlayer
And here is the code to call those functions:
// Cycle to next available player to make them the primary
public void CyclePlayer()
{
++_currentPlayer;
if (_currentPlayer >= _playerObjects.Count)
{
_currentPlayer = 0;
} // if
// Make current player a CompanionAI
AIToPlayer obj = _playerObj.GetComponent<AIToPlayer>();
obj.SwitchToAI();
// Make next player the current one
obj = _playerObjects[_currentPlayer].GetComponent<AIToPlayer>();
obj.SwitchToPlayer();
_playerObj = _playerObjects[_currentPlayer];
// Switch camera target
_playerCamera.GetComponent<vThirdPersonCamera>().SetMainTarget(_playerObj.transform);
} // CyclePlayer()
I am guessing there is some code somewhere that needs to be reinitialized or something like that, but I just haven't been able to find it.