- Home /
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?);
}
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.
Answer by Socapex · Jun 21, 2015 at 11:29 AM
nearTree.collider.gameObject
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?) ".
@imZeph Just wondering did you ever find a fix to this ? and if so how did you fix your code.
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.
RaycastHit = hit;
if(Physics.Linecast(startPoint, endPoint, out hit))
{
GameObject someObj = hit.collider.gameObject;
}
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 :
Raycasting from a gameobject will not ignore its own 2D collider
Raycasting does not "draw" a line, it merely casts one to detect collision. If you want to visualize it you can use Debug.DrawRay()
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 );
//
}
}
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
![](https://koobas.hobune.stream/wayback/20220613201219im_/https://answers.unity.com/themes/thub/images/avi.jpg)