Search Unity

How to use an object in script after being hit with an rayCast

Discussion in 'Scripting' started by pabu_, Jan 17, 2022.

  1. pabu_

    pabu_

    Joined:
    Aug 19, 2021
    Posts:
    3
    The title is kinda confusing but I have this code that is on the main camera that shoots a raycast and if I hit the object I want to put the object on the empty but I don`t know how to reference the object that I hit or how to make so it only hits objects that are on a certain layer upload_2022-1-17_12-31-1.png
    I have tryed to use GetComponent<Rigidbody>() but it doesn`t work upload_2022-1-17_12-34-37.png
    the blue dot is the empty / theDest
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please use code-tags when posting code, you don't need to be posting images of code, it always makes it impossible to refer to specific lines of code etc.

    If I'm following what you're asking, it looks like you've not read the documentation for Raycast because you're not looking at the result it provides you (hit) which is a RaycastHit. The API doc also shows you how to select which layers etc.
     
    pabu_ and Kurt-Dekker like this.
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    Don't forget to start by typing your question into Google. This is all extremely well-covered ground.

    Screen Shot 2022-01-17 at 7.48.00 AM.png
     
    SisusCo likes this.
  5. pabu_

    pabu_

    Joined:
    Aug 19, 2021
    Posts:
    3
    Thanks I am going to read it