0
\$\begingroup\$

INTRODUCTION:

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.

CODE:

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
        }
    }
}

This is the initiator script on the object that can be used (the chair in this example):

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

The scene is this: A quick description of what is going on; chair1 is the gameobject with the initiator script and has no colliders on it but is the parent of 2 colliders that are convex. FPSPlayer is the player controller, Playercam is the camera that shoots the main player camera shoot script. If you need any more information feel free to ask and I will provide it in an edit.

enter image description here

CONCLUSION:

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.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit.

It is the responsibility of the code that fired the ray to take the next action — objects hit by the ray have no idea they were hit by anything at all unless you write code to tell them so.

You likely want to do something a bit more like this:

// First, define an interface for all objects usable this way.
public interface IPlayerUsable {
    void Use();
}

// Implement that interface on each object's script.
public class PerspectiveChange : MonoBehaviour, IPlayerUsable {
    public Camera _camera;
    bool beingUsed;
    
    void Use() {
        beingUsed = true;
        _camera.gameObject.SetActive(true);
        Player.ganeObject.SetActive(false);
    }
}

// Now your ray routine can look something like this:
void Update() {
    if (Input.GetButtonDown("Use")) {
        RaycastHit hit; 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

        if (Physics.Raycast(ray, out hit, 10.0f)) {
            if (hit.collider.TryGetComponent(out IPlayerUsable usable)) {
                usable.Use();
            }
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ I remember you invited me to a gamedev chat on stackexchange but i completely forgot how to join it and I really suck at researching, so half of my questions already have answers somewere else even after an hour of me trying to find them, so being on the chat would be helpful \$\endgroup\$
    – Object
    Jun 21 at 8:10
  • \$\begingroup\$ Game Development Chat can be accessed by clicking the StackExchange icon in the top-right of the site and clicking the "chat" link next to the "Game Development" heading, then selecting the room you want to join. \$\endgroup\$
    – DMGregory
    Jun 21 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .