Hi, I have a controller script that is on two characters and when one character is active and I try to control him they both move. So I was just wondering if someone knows a way to fix this. Everything else works fine like that camera switching. If someone could help me that would be great thanks. These scripts are on a prefab that will be spawned in at the start of the scene.
public class MyFPSController : MonoBehaviour {
public float Speed;
public unitManager myUnitManagerScript;
public GameObject UnitBeingControlled;
void Start()
{
UnitBeingControlled = this.gameObject;
}
void Update()
{
if (myUnitManagerScript.GetComponent<unitManager>().InFP == true)
{
//Controls the unit movement
if (Input.GetKey(KeyCode.W))
{
UnitBeingControlled.transform.Translate(Vector3.forward * Time.deltaTime * Speed);
}
if (Input.GetKey(KeyCode.A))
{
UnitBeingControlled.transform.Translate(Vector3.left * Time.deltaTime * Speed);
}
if (Input.GetKey(KeyCode.S))
{
UnitBeingControlled.transform.Translate(Vector3.back * Time.deltaTime * Speed);
}
if (Input.GetKey(KeyCode.D))
{
UnitBeingControlled.transform.Translate(Vector3.right * Time.deltaTime * Speed);
}
}
}
}
public class unitManager : MonoBehaviour {
public bool hover = false;
public bool selected = false;
public bool NameMatches = false;
public bool InFP = false;
public bool FPSCameraEnabled = false;
public GameObject FPSCamera;
public GameObject MainCamera;
public GameObject Unit;
void Start () {
Unit = this.gameObject;
FPSCamera = this.gameObject.transform.GetChild(0).gameObject;
InFP = false;
}
void Update()
{
//Enters First Person
if (Input.GetKeyDown(KeyCode.Return))
{
InFP = true;
}
//Activates First Person Cmaera
if (hover == true && selected == true && InFP == true)
{
FPSCamera.active = true;
MainCamera.active = false;
}
//Leaves First Person
if(Input.GetKeyDown(KeyCode.Escape) && InFP == true)
{
MainCamera.active = true;
FPSCamera.active = false;
InFP = false;
//FPSCameraEnabled = false;
}
}
void OnMouseOver()
{
hover = true;
}
void OnMouseExit()
{
hover = false;
selected = false;
}
void OnMouseDown()
{
selected = true;
}
}