- Home /
!(not)Linecast not working _but Linecast works
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.
You didn't use a variable of type Transform for your playerObject like in the Script Ref did you?
No, I used gameObject. Here is the current setup for the above script (I didn't think Transform over GameObject was an issue, so didn't post it, sorry) :
var playerObject : GameObject;
// abbreviated Start()
playerObject = GameObject.FindWithTag ("Player");
Debug.Log("Player Position " + playerObject.transform.position);
I just tried Transform now, and it throws out an error for my 'FindWithTag'. thanks.
This is really weird ... I'm getting the same as you ...
Docs say: Returns true if there is any collider intersecting the line between start and end. This means that if we're negating it, it should return true if there aren't colliders intercepting the linecast. And yet, that's clearly not what's happening.
Possibly the docs are wrong?
mmm ,thats why I thought I would risk asking a silly question and post it.
I thought it would be assumed that this snippet of code is in a function. I have edited the question.
Answer by Kryptos · Apr 11, 2012 at 04:00 PM
Actually, the reason is quite simple. Looking at your code :
if (!Physics.Linecast (transform.position, playerObject.transform.position, rayHit))
{
Debug.Log("Player is Sighted : rayHit " + rayHit.collider.tag);
}
If Physics.Linecast returns false, this means that nothing was hit. Therefore, rayHit is not populated. So rayHit.collider.tag will trigger a NullReferenceException. Thus no log.
See http://unity3d.com/support/documentation/ScriptReference/RaycastHit-collider.html
Edit:
Does your object have a collider? If true, then there is always at least a collider : your object's collider itself.
You can use the optional layerMask to prevent your collider from being considered, assuming your are probing on objects in different layers. If not, try to send the linecast from a position that is outside your own collider.
Ah, but I tried this without a RaycastHit. No log either. Tried exactly as is in docs. :P
Here is my script:
var player : Transform;
function Update () {
transform.LookAt(player);
if (!Physics.Linecast(transform.position, player.position)) {
Debug.Log("No collisions");
} else {
Debug.DrawLine(transform.position, player.position, Color.red);
Debug.Log("There's colliders");
}
}
Returns "There's colliders" every time. :/
EDIT
Even without colliders on the player object (no normal colliders, character controller, rigidbody, etc.), still returns "There's colliders" ...
EDIT EDIT
Haha, scratch that ... it was re-enabling itself on runtime ... disabled from script and it's printing "No colliders" :)
There's your problem @alucardj ... if you want to call it with a negated Physics.Linecast, then all colliders (including gameObjects at start and end points) need to be disabled. :)
Check comment above. @$$anonymous$$ryptos is right ... and docs wrong. Unless of course the docs assume you have no colliders involved :D
Bit misleading nevertheless ...
I would not say that the doc is wrong. But unclear, misleading, definitely.
I'd say a large portion of the docs are misleading, for new users anyway ... cough cough $$anonymous$$athf.Lerp
Ahem ...
if (Nicolas$$anonymous$$usset == $$anonymous$$ryptos) {
alucardj.EditComment();
Debug.Log("Nicolas $$anonymous$$usset and $$anonymous$$ryptos are the same person!");
}
:)
Answer by Cinnax · Jan 16, 2013 at 05:07 AM
I know this is an older thread, but you should understand that the Scripting Reference given by Unity assumes that you want to do something if the linecast is not colliding with an object. If your intentions are to collect information about an object that your linecast "hits," then take out the not statement (!).
Just to be clear, linecast returns true if an object intersects the line between the origin and target. information about that object is stored in a Raycasthit variable (rayHit in the case of the OP). Remember, the contents of an if statement is only executed if the condition evaluates to true. If the linecast returns true (an object is hit) then !true becomes false and the contents of the if statement are not executed.
Your answer
Follow this Question
Related Questions
increase linecast length 0 Answers
[2D] Check if player is on the ground 4 Answers
Calculating angle using a raycastHit2D 1 Answer
RayCast or LineCast? 1 Answer
Linecast Layer Mask Declaration Problem 2 Answers