Hi, imagine a square game area, divided into 4 areas. Each area is tagged One, Two, Three & Four. I want to be able to detect which area my First Player Controller is over so that I can enable and disable certain functionality.
From the research I have done, Raycast, looking for a collision and then examining the Tags of the object that collided with seems to be the way to go, and I have so far created this code, which creates Debug output to prove it is working before I start adding the actual functionality I need.
void Update () {
RaycastHit hit;
Debug.Log ("(1) In menu controls update");
if (Physics.Raycast (transform.position, -Vector3.up, out hit))
{
float distanceToGround = hit.distance;
Debug.Log ("(2) distance to ground is" + distanceToGround);
if(hit.collider.CompareTag("Two"))
{ Debug.Log ("(3) Were over Two"); }
else
{ Debug.Log ("(4) Were not over Two"); }
} else
{ Debug.Log ("(5) dropped to else"); }
}
So this runs, and when moving around, I see Debug Lines 1, 2 with two demonstrating an appropriate distance to the ground, and then I see Debug Line 4, I never see lines 3 even when moving into Area Two or Debug Line 5 ever, which is fine.
The Tag is correctly set to Two, the Assets that are Tagged have a Mesh Collider on them.
Can anyone see what I am doing wrong, or can you suggest a more appropriate way of achieving something like this ?
Cheers
Craig