1
votes

Ok so I intend on putting multiplayer into my game and there will be multiple player objects with their own cameras.

Each player should be able to click on their own selectable objects in the scene via a raycast and not other players objects.

How would I go about comparing if the player's camera that made the raycast is allowed to select an object?

I might be approaching multiplayer and cameras and such all wrong so it'd be nice if anyone knew and could point that out.

If what I'm trying to say doesn't make sense then feel free to ask me, I'd be happy to elaborate.

1
Your question makes no sense. If you're performing a raycast from a camera (e.g. ScreenToWorldPoint or ScreenPointToRay) you already know what camera its coming from: the one you called the function on. - Draco18s no longer trusts SE
What I'm trying to say is that the objects in the scene have no idea which camera is casting a ray on it. Is there some kind of way I can tag them so the objects can compare the ray's information to it's own and see if it's on the same team and then select it? - Toxic Cookie
That still makes no sense. The object should not give a damn about what rays are hitting it. The ray doesn't even cause any code to be called on any monobehaviours attached to the object. The code CALLING the raycast on the other hand you already have everything you need (except which object the raycast lands on (except that, yes you do: the raycasthit result contains it)). - Draco18s no longer trusts SE
Of course! Then how do you check to see what object the ray hit? - Toxic Cookie
The RaycastHit object returned as an out parameter. - Draco18s no longer trusts SE

1 Answers

3
votes

It could be I misunderstand you, but,

when you raycast, you quite simply "make a ray".

(Actually, it could be anywhere at all - it may have nothing to do with a camera. For example, when you do suspension for cars, you make rays which point downwards towards the ground for a certain reason - you know?)

So the answer to your question is simply, for camera X, you indeed make the ray come from that camera!

https://docs.unity3d.com/Manual/CameraRays.html

Hope that's what you meant.

Simply from the Unity example ...

   RaycastHit hit;
    Ray ray = camera.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit)) {
        Transform objectHit = hit.transform;

        // Do something with the object that was hit by the raycast.
    }

I think you're just trying to send a message TO that object which was hit. Very simple:

Let's say you have a script named "Robot.cs" on the object you HIT.

Robot hitRobot = objectHit.GetComponent<Robot>();

you now have that component on the object you hit.

Let's say there's a funtion "IWasHit()" in Robot.cs

So the next step is just

hitRobot.IWasHit();

You see? Easy! :)

Enjoy