My question relates to using !Physics.Linecast compared to Physics.Linecast.
I just tried using Linecast for the first time. In the Unity Scripting reference, the example is given :
var target : Transform;
function Update() {
if (!Physics.Linecast (transform.position, target.position)) {
ProcessData.AndDoSomeCalculations();
}
}
So I applied this with (on false statement):
var playerObject : GameObject;
function Start() {
playerObject = GameObject.FindWithTag ("Player");
Debug.Log("Player Position " + playerObject.transform.position);
}
function Update() {
Debug.DrawLine (transform.position, playerObject.transform.position, Color.red);
//if (!Physics.Linecast (transform.position, playerObject.transform.position))
if (!Physics.Linecast (transform.position, playerObject.transform.position, rayHit))
{
Debug.Log("Player is Sighted : rayHit " + rayHit.collider.tag);
}
}
However , it is not printing anything at all , whether Player is in line-of-sight or not. Whereas this works (on true statement):
var playerObject : GameObject;
function Start() {
playerObject = GameObject.FindWithTag ("Player");
}
function Update() {
Debug.DrawLine (transform.position, playerObject.transform.position, Color.red);
if (Physics.Linecast (transform.position, playerObject.transform.position, rayHit))
{
if (rayHit.collider.tag == "Player")
{
Debug.Log("Player is Sighted : rayHit " + rayHit.collider.tag);
}
}
}
I have a working solution , but I cannot understand why my first script (mirrored from the Unity Script Reference) is not working.
This is a minor question , so please just leave a comment , so I can remove this question when I understand what I am missing here.
Many thanks.
- Edited [1] to include var setup and relevant Start info.
- Edited [2] to clarify where the code is in my script.