Skip to main content
Rollback to Revision 1
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

How to make an object do something whendetect if hit by a specific raycast from object with name/tag

In unity, I want to make a dynamic, object use system. Where the main player camera shoots a raycast when left mouse button is clicked and if thean object that can be used for example a chair is hit by that specific raycast specifically from the player hits an object with a tag "use"(there could be other raycasts being shot out from other objects and the player), then that object will do whatever its function isrun its action, in the chairs case it would disable the player controller and camera and enable its own camera.

This is the main use script which is responsible for shooting the raycast from the player camera shoot script:

public interface IPlayerUsable
{
    public void Use();
}
public class use : MonoBehaviour
{
    public Camera cam; //player camera

    void Update()
    {
        if (Input.GetKey(KeyCode.Mouse0) == true) //whenif left clickmouse button down
        {
            RaycastHit hit;
     
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); // shootshoots raycast from camera mouse position (itscursor lockmode is locked so its center)

 from centre of screen)
        if (Physics.Raycast(ray, out hit, 10.0f)) // shoots raycast a distance of 10
            {
                if (hit.collider.TryGetComponent(out IPlayerUsable usable)) // im not sure what this really means but I would assumenothing itmore detectsneeded thehere, publicits interfacenow componentup ofto the object it hit
                {
                    usable.Use(); // it runs the functionto ofdo the objects public interface
                    Debug.Log("Shot a ray"); // tells you if itaction didonce it
                }
             
           detects }this
        }
 
    }
}
public class PerspectiveChange : MonoBehaviour, IPlayerUsable
{
   private publicvoid HitByRay(GameObject Player;gameObject) //detects thea playerraycast controllerhitting itself
{
    publicif(gameObject.name GameObject== chaircam;"Playercam") // the chair camera controller
    private booldetects beingusedif =the false;raycast //is obejct beingfrom used?
camera
    public void Use(){
    {
     //does the action Debug.Log("Ithis wascan hitalso byjust activate a Ray");boolean //tellsso the consoleaction ifcan itoccur recievedin thea hitdifferent void):
        beingused = true; // is beingused
        chaircam.gameObject.SetActive(true); //shows the chair camera
        Player.gameObject.SetActive(false); 
 //disables the player controller and camera  Debug.Log("I was hit by a Ray");
    }
}

My goal is to make an object do what it does if it hasbe able to detect the tag "use" when it is hit by a left click triggered raycast that is being shot from the player camera. However,an object and detect the objects name that shot out that raycast and if that name is not being sent in the codename we want then it does something.

How to make an object do something when hit by a specific raycast

In unity, I want to make a dynamic, object use system. Where the main player camera shoots a raycast when left mouse button is clicked and if the raycast from the player hits an object with a tag "use" then that object will do whatever its function is, in the chairs case it would disable the player controller and camera and enable its own camera.

This is the main use script which is responsible for shooting the raycast from the player camera:

public interface IPlayerUsable
{
    public void Use();
}
public class use : MonoBehaviour
{
    public Camera cam; //player camera

    void Update()
    {
        if (Input.GetKey(KeyCode.Mouse0) == true) //when left click down
        {
            RaycastHit hit;
            Ray ray = cam.ScreenPointToRay(Input.mousePosition); // shoot raycast from camera mouse (its locked so its center)

            if (Physics.Raycast(ray, out hit, 10.0f)) // shoots raycast a distance of 10
            {
                if (hit.collider.TryGetComponent(out IPlayerUsable usable)) // im not sure what this really means but I would assume it detects the public interface component of the object it hit
                {
                    usable.Use(); // it runs the function of the objects public interface
                    Debug.Log("Shot a ray"); // tells you if it did it
                }
             
            }
        }
 
    }
}
public class PerspectiveChange : MonoBehaviour, IPlayerUsable
{
    public GameObject Player; // the player controller
    public GameObject chaircam; // the chair camera controller
    private bool beingused = false; //is obejct being used?

    public void Use()
    {
        Debug.Log("I was hit by a Ray"); //tells the console if it recieved the hit
        beingused = true; // is beingused
        chaircam.gameObject.SetActive(true); //shows the chair camera
        Player.gameObject.SetActive(false); //disables the player controller and camera
    }
}

My goal is to make an object do what it does if it has the tag "use" when it is hit by a left click triggered raycast from the player camera. However, the raycast is not being sent in the code.

How to detect if hit by raycast from object with name/tag

In unity, I want to make a dynamic, object use system. Where the main player camera shoots a raycast when left mouse button is clicked and if an object that can be used for example a chair is hit by that specific raycast specifically from the player (there could be other raycasts being shot out from other objects and the player), then that object will run its action, in the chairs case it would disable the player controller and camera and enable its own camera.

This is the main player camera shoot script:

    public Camera cam;
void Update()
{
    if (Input.GetKey(KeyCode.Mouse0) == true) //if left mouse button down
    {
        RaycastHit hit;  
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); //shoots raycast from mouse position (cursor lockmode is locked so its from centre of screen)
        if (Physics.Raycast(ray, out hit, 10.0f)) // shoots raycast
        {
          //nothing more needed here, its now up to the object to do the action once it detects this
        }
    }
}
  private void HitByRay(GameObject gameObject) //detects a raycast hitting itself
{
    if(gameObject.name == "Playercam") //detects if the raycast is from camera
    {
        //does the action (this can also just activate a boolean so the action can occur in a different void):
        beingused = true;
        chaircam.gameObject.SetActive(true);
        Player.gameObject.SetActive(false); 
        Debug.Log("I was hit by a Ray");
    }
}

My goal is to be able to detect the raycast that is being shot from an object and detect the objects name that shot out that raycast and if that name is the name we want then it does something.

edited title
Link
Object
  • 289
  • 1
  • 6
  • 13

How to make a hit obejctan object do something when hit by a specific raycast from player

added 53 characters in body
Source Link
Object
  • 289
  • 1
  • 6
  • 13

My goal is to make an object do what it does if it has the tag "use" when it is hit by a left click triggered raycast from the player camera. However, the raycast is not being sent in the code.

My goal is to make an object do what it does if it has the tag "use" when it is hit by a left click triggered raycast from the player camera

My goal is to make an object do what it does if it has the tag "use" when it is hit by a left click triggered raycast from the player camera. However, the raycast is not being sent in the code.

added 125 characters in body
Source Link
Object
  • 289
  • 1
  • 6
  • 13
Loading
added 412 characters in body; edited title
Source Link
Object
  • 289
  • 1
  • 6
  • 13
Loading
Source Link
Object
  • 289
  • 1
  • 6
  • 13
Loading