Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AlucardJay · Apr 11, 2012 at 02:49 PM · linecast

!(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.

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kleptomaniac · Apr 11, 2012 at 03:01 PM 0
Share

You didn't use a variable of type Transform for your playerObject like in the Script Ref did you?

avatar image AlucardJay · Apr 11, 2012 at 03:08 PM 0
Share

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.

avatar image Kleptomaniac · Apr 11, 2012 at 03:21 PM 0
Share

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?

avatar image AlucardJay · Apr 11, 2012 at 03:22 PM 0
Share

mmm ,thats why I thought I would risk asking a silly question and post it.

avatar image AlucardJay · Apr 11, 2012 at 03:53 PM 1
Share

I thought it would be assumed that this snippet of code is in a function. I have edited the question.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kleptomaniac · Apr 11, 2012 at 04:01 PM 1
Share

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. :)

avatar image Kleptomaniac · Apr 11, 2012 at 04:20 PM 1
Share

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 ...

avatar image Kryptos · Apr 11, 2012 at 04:22 PM 2
Share

I would not say that the doc is wrong. But unclear, misleading, definitely.

avatar image Kleptomaniac · Apr 11, 2012 at 04:23 PM 1
Share

I'd say a large portion of the docs are misleading, for new users anyway ... cough cough $$anonymous$$athf.Lerp

avatar image Kleptomaniac · Apr 11, 2012 at 04:40 PM 1
Share

Ahem ...

 if (Nicolas$$anonymous$$usset == $$anonymous$$ryptos) {
     alucardj.EditComment();
     Debug.Log("Nicolas $$anonymous$$usset and $$anonymous$$ryptos are the same person!");
 }

:)

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges