I am trying to do a SphereCast to check if my Player is visible to the Enemy, and the Enemy has a clear line-of-sight to the player for the full width of the Enemy’s body.
After a frustrating situation learning Linecast (where i actually lost my cool, but Kryptos and Klep helped me [sorry again Kryptos]), I got it working. (Link to Qn : http://answers.unity3d.com/questions/238616/notlinecast-not-working-but-linecast-works.html)
However I discovered I need a SphereCast to check the Enemy can actually have a line-of-sight that it’s whole body could fit through.
Here is the snippet of relevant code (if I’ve ommited any useful info, please let me know) :
// -- Look for Player --
//Debug.DrawLine(transform.position, playerObject.transform.position, Color.red); // Previous Linecast
//if (Physics.Linecast(transform.position, playerObject.transform.position, rayHit)) // Working, but change to Spherecast
var castDistance = (playerObject.transform.position - transform.position).magnitude;
if (Physics.SphereCast(transform.position, 0.75, playerObject.transform.position, rayHit, castDistance))
{
Debug.DrawRay(transform.position, (playerObject.transform.position - transform.position), Color.green); // show path of SphereCast
Debug.Log("rayHit.collider.tag " + rayHit.collider.tag); // print collider info
// -- Set WayPoint as Player if Player is Sighted --
if ((rayHit.collider.tag == "Player") && (enemyState == 1)) // enemyState = 1 for normal travel , 2 for chasing Player. This part is working fine (enemyState is = 1)
{
Debug.Log("Player is Sighted : wayPointTarget " + wayPointTarget + " : rayHit " + rayHit.collider.tag);
enemyState = 2; // etc ....
}
}
Here are some screenshots to help illustrate my problem (Enemy is Red , Player is Blue) :
Screenshot 1 : situation normal. Player is obscured by Wall (blue DrawLine is Enemy avoiding Wall part of script) .
http://www.alucardj.net16.net/unityquestions/spherecast%20screenshot%201.png
Screenshot 2 : Enemy has cleared Wall . but Spherecast is not returning Player’s tag .
http://www.alucardj.net16.net/unityquestions/spherecast%20screenshot%202.png
Screenshot 3 : Enemy travels PAST Player to waypoint . Spherecast is still not returning Player’s tag .
http://www.alucardj.net16.net/unityquestions/spherecast%20screenshot%203.png
Screenshot 4 : Infact the only time that the Spherecast returns the Player’s tag is when the Enemy collides with the Player .
http://www.alucardj.net16.net/unityquestions/spherecast%20screenshot%204.png
I have done some extensive searching and reading (example links below), and while I understand and want to cast a sphere down a line (green line) and return true if intersecting a collider (be it Wall or Player), I cannot understand why this effect is not being achieved with my script.
Any comments or help on what I am doing wrong would be greatly and humbly appreciated. Thanks , Jay.
Example search results (reading material) :
http://answers.unity3d.com/questions/32713/understanding-spherecasting.html
http://www.theantranch.com/Unity/Entries/2011/3/29_Understanding_Spherecasts_in_Unity.html
http://answers.unity3d.com/questions/32235/spherecast-casts-from-the-circumference-not-the-ce.html
and of course : file:///C:/Program%20Files/Unity/Editor/Data/Documentation/Documentation/ScriptReference/Physics.SphereCast.html