Controller script active on both characters.
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;
}
}
Answer by LuckierBread · Jul 16, 2016 at 04:45 AM
I'm no pro but it looks like the problem is that both characters are the same prefab.
If that is the case then they are both identifying themselves as UnitBeingControlled because they both run the following on startup.
void Start()
{
UnitBeingControlled = this.gameObject;
}
You might need to have two prefabs with slightly different scripts identifying them by tag or something.
Well that's the thing is that the script is identifying them properly as different characters, but it just dosent want to work.
Ok just tested what you said and it might work ill let you know. I didn't even think of that thanks.
Ok so when I add the unit$$anonymous$$anager script to the fpscontroller script in the inspector, it controls both of the characters(not prefabs) but if I don't define anything only one character moves.
Ok I figured it out, I had to add a few true false statements lol so easy, but I guess I just overthought it. Thanks for the help!
Your answer
Follow this Question
Related Questions
MoveTowards is curving for no reason 0 Answers
Raycast Check in GetKey? 0 Answers
How to move whilst airborne (using standard third person character script)? 0 Answers
how to move only one object ? 1 Answer