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 ImZeph · Jun 21, 2015 at 11:13 AM · 2dlinecast

How to find the object that the linecast hits?

Hello. I want to destroy the tree that the linecast hits when I press "E". How do I do that? This is in 2D!

         Debug.DrawLine(transform.position, treeSight.position, Color.black);
         nearTree = Physics2D.Linecast(transform.position, treeSight.position, 1 << LayerMask.NameToLayer("Tree"));
 
         if (Input.GetKeyDown (KeyCode.E) && nearTree == true) {
             Destroy(//What do I put in here?);
         }

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AurimasBlazulionis · Jan 26, 2017 at 06:29 PM

You need to set linecast variable from the output of the linecast

 hit = Physics2D.Linecast(transform.position, treeSight.position, 1 << LayerMask.NameToLayer("Tree"))) {
 
 if (hit.collider != null)
      Destroy(hit.collider.gameObject);
 

Notice hit = , define RaycastHit2D hit before calling this function. It will store hit information you need. Also, you can simply create a variable of type LayerMask and select your trees layer there. And use it as the mask. It makes things easier when dealing with multiple layers.

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
avatar image
-1

Answer by Socapex · Jun 21, 2015 at 11:29 AM

nearTree.collider.gameObject

Comment
Add comment · Show 4 · 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 ImZeph · Jun 21, 2015 at 06:44 PM 0
Share

I'm sorry but it doesn't work.

It says: "Assets/scripts/character.cs(43,42): error CS1061: Type bool' does not contain a definition for collider' and no extension method collider' of type bool' could be found (are you missing a using directive or an assembly reference?) ".

avatar image Nafroman ImZeph · May 15, 2016 at 10:50 PM 0
Share

@imZeph Just wondering did you ever find a fix to this ? and if so how did you fix your code.

avatar image MagJ99 · Jan 26, 2017 at 03:05 PM 0
Share

I would be interested in the answer as well. I absolutely need a solution. I wrote already an answer but nobody seem to answer it.

avatar image PizzaPie MagJ99 · Jan 26, 2017 at 06:22 PM 0
Share
 RaycastHit = hit;
 if(Physics.Linecast(startPoint, endPoint, out hit))
 {
         GameObject someObj = hit.collider.gameObject;
 }



avatar image
0

Answer by Koen-Matthijs · Jan 26, 2017 at 08:34 PM

Hi

Detecting a hit on a collider is not done using LineCast but achieved using RayCasting. However, in 2D there's a few things to keep in mind :

  1. Raycasting from a gameobject will not ignore its own 2D collider

  2. Raycasting does not "draw" a line, it merely casts one to detect collision. If you want to visualize it you can use Debug.DrawRay()

  3. The object you want to "hit" during the raycast must have a 2D collider

Here's an example:

 void Update() {
     // Disable the object's own collider to avoid a false-positive hit
     // Remove this if your object doesn't have a collider
     collider2D.enabled = false;

     // Raycast for a hit in the forward direction at a maximum distance of 100 units
     RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector3.forward, 100f, 1 << LayerMask.NameToLayer("Tree"));

     // Re-enable the object's own collider
     // Remove this if your object doesn't have a collider
     collider2D.enabled = true;

     // Visualize the ray for debugging purposes
     Debug.DrawRay(transform.position, Vector3.forward * 100f, Color.magenta);
 
     // Report the hit to the console
     if (hit.collider != null) {
         print(hit.collider.name + " is in front of " + gameObject.name);

         // To destroy the object you hit you can uncomment the line below
         //
         // Destroy( hit.collider.gameObject );
         //
     }
 }
Comment
Add comment · Show 1 · 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 PizzaPie · Jan 26, 2017 at 09:31 PM 0
Share

Not really Linecast and Raycast are pretty much the same, the only difference is that on Linecast you need to know the end point and on the Raycast you need to know the direction but aside that they both provide an out RaycastHit param . So with both you can have access to the hit.collider. Also the Raycast and Linecast WILL ignore their own colliders, it is mentioned on the API docs on Raycast. Best regards.

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

8 People are following this question.

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

Related Questions

Check if 2D Player is grounded with Physics2D.Linecast 1 Answer

Raycast Physics2D not working as expected 1 Answer

Phisycs2D.Linecast doesn't work correctly 1 Answer

Linecast, rotate end point relative to objects rotation 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer


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