Hi all,
I’m quite new to c# and unity so please excuse my maybe noobish question^^
Assumed Problem: OnTriggerExit doesn’t get called, although sphere is moved away.
Details:
I’m creating kind of a strategy map, where multiple players(2 at the moment) can move to defined places(towns) in a turn-based manner.
I’m using a sphere trigger with attached rigidbody and a plane for each town(also triggers) to see which of the towns are close enough to move to. The sphere is set to the position of the player, who is changed when he reaches his target. The towns in reach are lit up for the player to see where he can move to.I have 2 scripts for this. One is Bodenhaftung.cs (not attached I think) and the other one is at the moment still called newBehaviourScript (attached to every plane/town in the scene).
Most part of the script works well, but the one town the other player last arrived at doesn’t get disabled after the player switch. I checked and saw that OnTriggerExit doesn’t get called even though the sphere doesn’t touch the plane anymore. What’s even better: OnTriggerStay still seems to get called…
I tried the whole thing with OnTriggerEnter but then I can’t seem to move to places where the other player could also move to…
I’m very confused here so I’d be glad to get some help^^
If you need any further info, just tell me^^
Also I’d like to know why this isn’t working because I’d like to fix things myself and learn something from this mistake not let my work done by others^^
thank you in advance
Code:
public class Bodenhaftung : MonoBehaviour {
public static Component Grund;
public static GameObject Spieler;
public static GameObject Kugel;
public static GameObject Ziel;
public static int Zielpunkt;
public static float distanz;
// Use this for initialization
public void Start () {
Spieler = GameObject.Find("Constructor");
Kugel = GameObject.Find("Sphere1");
distanz = 0;
}
// Update is called once per frame
void Update () {
}
}
public class NewBehaviourScript : Bodenhaftung {
public Color colortrans=Color.black;
IEnumerator distrechner()
{
while (Zielpunkt==1)
{
float translation = Time.deltaTime * 10;
Spieler.transform.Translate(0, 0, translation);
distanz = Vector3.Distance(Ziel.transform.position,Spieler.transform.position);
if (distanz >= -1 && distanz <=1) {
Zielpunkt = 0;
if (Spieler == GameObject.Find("Constructor"))
{
Spieler = GameObject.Find("Constructor2");
}
else
{
Spieler = GameObject.Find("Constructor");
}
yield return Zielpunkt;
break;
}
yield return 0;
}
}
void OnMouseDown(){
if (distanz >= -1 && distanz <=1 && renderer.enabled==true)
{
Ziel = gameObject;
Spieler.transform.LookAt(Ziel.transform, Vector3.up);
Zielpunkt = 1;
StartCoroutine(distrechner());
}
}
void OnTriggerStay(Collider Kugel)
{
if (Zielpunkt == 0)
{
if (Ziel == gameObject) renderer.material.SetColor("_Color", Color.red);
else renderer.material.SetColor("_Color", Color.green);
renderer.enabled = true;
}
else
{
renderer.enabled = false;
}
}
void OnTriggerExit(Collider Kugel)
{
renderer.enabled = false;
}
void Update(){
Kugel.transform.position = Spieler.transform.position;
}
}
EDIT: Added a picture so it’s a little bit easier to see
http://imageshack.us/photo/my-images/841/mapdq.jpg/
As you can see the guy in the left sees his possible targets but also the last position his counterpart went to (up right).
Uploading a picture seems to be tricky…any hints?