[C#] [2D] 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?);
         }
Destroy(nearTree.transform.gameObject);

This is my full code, just in case it helps.

using UnityEngine;
using System.Collections;

public class character : MonoBehaviour {

        public float speed = 4f;
        public float jumpHeight = 200f;

        public Transform treeSight;
        public Transform jumpRay;

        public bool nearTree = false;
        public bool onGround = true;

    void Start () {
       
    }

    void Update () {

            // Movement

        if (Input.GetKey (KeyCode.D)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.A)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }

            // Tree chopping

        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();
        }

            // Jumping

        Debug.DrawLine(transform.position, jumpRay.position, Color.green);
        onGround = Physics2D.Linecast(transform.position, jumpRay.position, 1 << LayerMask.NameToLayer("Ground"));

        if (Input.GetKeyDown (KeyCode.Space) && onGround == true) {
            GetComponent<Rigidbody2D>().AddForce (Vector2.up * jumpHeight);
        }
    }

}

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.

1 Like

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.

Here you go:

using UnityEngine;
using System.Collections;
public class character : MonoBehaviour {
        public float speed = 4f;
        public float jumpHeight = 200f;
        public Transform treeSight;
        public Transform jumpRay;
        public RaycastHit2D nearTree;
        public RaycastHit2D onGround;

    void Update () {
            // Movement
        if (Input.GetKey (KeyCode.D)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.A)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }
            // Tree chopping
        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) {
            Destroy(nearTree.transform.gameObject);
        }
            // Jumping
        Debug.DrawLine(transform.position, jumpRay.position, Color.green);
        onGround = Physics2D.Linecast(transform.position, jumpRay.position, 1 << LayerMask.NameToLayer("Ground"));
        if (Input.GetKeyDown (KeyCode.Space) && onGround) {
            GetComponent<Rigidbody2D>().AddForce (Vector2.up * jumpHeight);
        }
    }
}

Yep, from docs:

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.

Lol, can you post the code that you finally use here?

Those are the errors that I get when using the version of the code that you supplied.

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);

You need the code from Post #8, not from Post #4

I’m so f-ing sorry. :smile: 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.

No problem:)

Oh, it was hidden inside Raycast2D.collider…