This is going to be an odd question as I’m essentially asking why something works, instead of why it could be broke.
I am creating a system in which two objects, a prey and a predator react to each other. In order to get these behaviours working I have had to make sure the objects can ‘see’ each other. I’ve begun with my prey object and have a raycast set up, checking for an object in range and then drawing a raycast line in debug to show the path between them. This is all handled within this method
bool HasPreySeenPredator() { //code to manage direction of prey and whether predator in range bool preySeenPredator; RaycastHit hit; Vector3 fwd = transform.TransformDirection(1,0,0); //fwd holds the orientation of the object's 'face' if(Physics.Raycast(transform.position, fwd, out hit, sightRange)) { preySeenPredator = true; Debug.Log("There is something in range. Distance: " + hit.distance + ". Object: "); Debug.DrawLine(transform.position, hit.point); } else {preySeenPredator = false;} return preySeenPredator; }
This is put together with the help of the Physics.Raycast references in unity’s online help files.
I have various objects within my scene to which the raycast does not react to. I have the prey tagged as ‘prey’ and the predator as ‘predator’ but haven’t used their tags in the code. My other objects are untagged and tagging them as ‘predator’ does not get the prey’s raycast to hit it.
Can anyone explain to me why this works?