Post by redsam121 on Oct 20, 2022 13:04:11 GMT
I'm working a party system in which you can switch between characters during battle. This involves disabling and re-enabling the AI controller. However this has led to certain bugs to appear such as companions locking up and stuck in animations. My prognosis is that after I've re enabled the AI scripts, they are resuming their previous actions which are now invalid and causing them become out of sync and lock up.
I've already solved this situation for the fsm behavior using this code to reset it:
mem.GetComponent<vFSMBehaviourController>().ResetFSM();
But I now need a way to reset the vControlAIMelee, as if I've just started the game and it'll redo detecting and attacking calculations.This is my party manager so far (it's currently only fixed to 2 players for testing):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCamera;
using Invector.vCharacterController;
using Invector.vCharacterController.AI;
using Invector.vCharacterController.AI.FSMBehaviour;
using Invector.vCharacterController.vActions;
using Invector;
using UnityEngine.AI;
public class PartyManager2 : MonoBehaviour
{
InputDefault input;
public GameObject[] member;
public vThirdPersonCamera cam;
public int index = 1;
public int previous;
private void Awake()
{
input = new InputDefault();
}
void Start()
{
index = Mathf.Clamp(index, 1, member.Length);
StartCoroutine(LateStart());
}
IEnumerator LateStart()
{
yield return new WaitForSeconds(0.025f);
enableActiveMode(member[0]);
enableAutoMode(member[1]);
}
void Update()
{
if (member.Length > 1)
{
if (Dup())
{
previous = index;
index++;
if (index > member.Length)
{
index = 1;
}
enableActiveMode(member[index - 1]);
enableAutoMode(member[previous - 1]);
}
if (Ddown())
{
previous = index;
index--;
if (index <= 0)
{
index = member.Length;
}
enableActiveMode(member[index - 1]);
enableAutoMode(member[previous - 1]);
}
}
}
void enableActiveMode(GameObject mem)
{
cam.SetMainTarget(mem.transform);
//Enable Controller
mem.GetComponent<vShooterMeleeInput>().SetLockBasicInput(false);
mem.GetComponent<vShooterMeleeInput>().SetLockCameraInput(false);
mem.GetComponent<vShooterMeleeInput>().SetLockMeleeInput(false);
mem.GetComponent<vShooterMeleeInput>().SetLockShooterInput(false);
mem.GetComponent<vShooterMeleeInput>().lockInput = false;
mem.GetComponent<vShooterMeleeInput>().isActive = true;
//Disable AI
mem.GetComponent<vControlAIMelee>().enabled = false;
mem.GetComponent<NavMeshAgent>().enabled = false;
mem.GetComponent<vAIHeadtrack>().enabled = false;
mem.GetComponent<vFSMBehaviourController>().isStopped = true;
}
void enableAutoMode(GameObject mem)
{
//Disable Controller
mem.GetComponent<vShooterMeleeInput>().SetLockBasicInput(true);
mem.GetComponent<vShooterMeleeInput>().SetLockCameraInput(true);
mem.GetComponent<vShooterMeleeInput>().SetLockMeleeInput(true);
mem.GetComponent<vShooterMeleeInput>().SetLockShooterInput(true);
mem.GetComponent<vShooterMeleeInput>().lockInput = true;
mem.GetComponent<vShooterMeleeInput>().isActive = false;
//Enable AI
mem.GetComponent<vControlAIMelee>().enabled = true;
//mem.GetComponent<vControlAIMelee>().ResetAIController();
mem.GetComponent<NavMeshAgent>().enabled = true;
mem.GetComponent<vAIHeadtrack>().enabled = true;
mem.GetComponent<vFSMBehaviourController>().isStopped = false;
mem.GetComponent<vFSMBehaviourController>().ResetFSM(); //Set AI back to default state
}
bool Dup()
{
return input.Default.Up.WasPerformedThisFrame();
}
bool Ddown()
{
return input.Default.Down.WasPerformedThisFrame();
}
private void OnEnable()
{
input.Default.Enable();
}
private void OnDisable()
{
input.Default.Disable();
}
}