I’ve put together a script which lets me pick between multiple players in a list which activates the selected player and deactivates the others and their movement scripts etc. The issue I’m having is players always spawn back to start of level or wherever there were last deactivated. I want new player to replace the previous players position. My character swap script is attached to a parent gameobject and all players are children of that object.
I’m still learning but I’m guessing I need a vector2 or transform component in there somewhere but I can’t figure out where and correct syntax? Any help would be much appreciated.
Thanks
Here is my characterSwap Script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CharacterSwap : MonoBehaviour
{
public GameObject character;
public List possibleCharacters;
public int whichCharacter;
public CinemachineVirtualCamera cam;
void Start()
{
if(character == null && possibleCharacters.Count >= 1)
{
character = possibleCharacters[0];
}
Swap();
}
void Update()
{
ProcessInput();
}
private void ProcessInput()
{
if (Input.GetKeyDown(KeyCode.Q))
{
if (whichCharacter == 0)
{
whichCharacter = possibleCharacters.Count - 1;
}
else
{
whichCharacter -= 1;
}
Swap();
}
if (Input.GetKeyDown(KeyCode.E))
{
if (whichCharacter == possibleCharacters.Count - 1)
{
whichCharacter = 0;
}
else
{
whichCharacter += 1;
}
Swap();
}
}
public void Swap()
{
character = possibleCharacters[whichCharacter];
character.GetComponent<NewPlayer>().enabled = true;
for (int i = 0; i < possibleCharacters.Count; i++)
{
//disable movement script
if (possibleCharacters *!= character)*
{
possibleCharacters*.GetComponent().enabled = false;*
}
//deactivate other characters
possibleCharacters[whichCharacter].SetActive(false);
whichCharacter++;
if (whichCharacter >= possibleCharacters.Count)
whichCharacter = 0;
possibleCharacters[whichCharacter].SetActive(true);
//character spawn to previous characters position ???
}
cam.Follow = character.transform;
}
}