- Home /
Other
Turning Off PlayerMovement Script when character isn't active?
I'm making an X-men Legends type rpg and I have a script where the camera switches to the selected character, the only problem is now all the characters move when I want to control just the selected character. I'm stumped can someone assist me with this?
public GameObject[] characters;
GameObject currentCharacter;
int charactersIndex;
void Start () {
charactersIndex = 0;
currentCharacter = characters[0];
}
void Update () {
if(Input.GetKeyDown("space"))
charactersIndex++;
if(charactersIndex == characters.Length){
charactersIndex = 0;
}
GameObject.Find("Main Camera").GetComponent<CameraFollow>().target = characters[charactersIndex].transform;
currentCharacter = characters[charactersIndex];
}
Answer by hexagonius · Dec 01, 2018 at 12:17 PM
If each player has a movement script then set it enabled = false before you switch to the next. You could also read about the concept of a PlayerController from Unreal Engine, which uses one input class that routes that input to any one character at any given time in the game:
https://docs.unrealengine.com/en-US/Gameplay/Framework/Controller/PlayerController
Thanks for the reply! I tried that in the code, but I may have screwed up the code. Now when I switch throught all of the characters are still in idle.
currentCharacter.GetComponent<Player$$anonymous$$ovement>().enabled = false;
characters[charactersIndex].GetComponent<Player$$anonymous$$ovement>().enabled = true;
I fixed it I added extra code in the LateUpdate. Thanks so much!!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Align Player to Regional Up Direction, Without Altering Look Direction Dramatically 0 Answers
When stopping Rigidbody.velocity 1 frame stutter C# 0 Answers
Transform player from one position to another after completing a goal 1 Answer