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?);
}
odd, Physics2D.Linecast() should return a RayCastHit2D object which would mean @ANTARES_XXI code would work… but from those errors it looks like it is returning a bool (like the Physics.Linecast() function does…)
RayCastHit2D maybe has a bool cast override, nearTree is defined as a bool.
You shouldn’t store nearTree as a bool, you should store it as RayCastHit2D, then you could do what @ANTARES_XXI suggests.
I’m really, really sorry but can you please give me a snippet of the code that I should use? I tried it out but how would I check that I’m near a tree when I press E. That’s the reason why nearTree is a boolean.
Note that some functions that return a single RaycastHit2D will leave the collider as NULL which indicates nothing hit. RaycastHit2D implements an implicit conversion operator converting to bool which checks this property allowing it to be used as a simple condition check for whether a hit occurred or not.
Not sure, cause there are no 45 lines of code in my solution, but there’s en error in the 45 line on your screenshot…
So just copy and paste your code here again please.
I skip a few lines to make my code easily readable. For some reason when posting it here it deletes those lines. Look at Post #4. It’s all the same just in line #45 it’s - Destroy(nearTree.transform.gameObject);
I’m so f-ing sorry. I didn’t notice that you changed out the bools to RayCastHit2D’s and I thought that the code was exactly the same. Sorry again and thank you.