Raycasting for two cameras.

Hi everyone

I have a two camera setup (sort of splitscreen) The main camera for the full screen and a compass camera that shows a 3d compass at bottom left of the screen. I need raycasting to work in both. But instead of “camera.main” I can’t put anything else in the code, because it gives me errors.

if (Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit))

If I tag the compass camera as “Main Camera” the raycast works, but stops working in the main view!

Hope this is clear and really hope someone can help.

Thanks

camera.main is not really a tag; its a reference to the camera tagged ‘main’. As such, there are no similar builtin references to other cameras with other tags.

To get a reference to a camera with tag “Camera A” :

var CamA = GameObject.FindWithTag (“Camera A”);

Hi Kaelis

Thanks for the reply. But the compass camera still doesn’t register the mouse click in it’s view.

What happens when you have a two player game in split screen and in both cameras you need raycast to work? How is that set up?

Cheers

Hello forum

Apologies for bumping this, but I’m still having problems making this work.
I tried isolating cameras to see different layers and placed the compass on a separate layer, but can’t get my head around the raycasting issue.

Below is the code I’m using for the compass object. It is getting the raycast from the MainCamera, but the compass is viewed from another camera and therefore it is not registering the mouse click, unless I tag it as MainCamera. If I do this however, clickable objects don’t work in the main view… Please help!

var numberAverages : int = 3; 

private var originalRotation : Quaternion; 
private var offsetRotation : Quaternion; 

// Make sure there is always a Rigidbody 
@script RequireComponent (Rigidbody) 
@script RequireComponent (SphereCollider) 


function Awake () { 
    
   numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages); 
    
} 


function OnMouseDown () { 
   var hit : RaycastHit; 
   var dir : Vector3; 
    
   // Stop spinning 
   rigidbody.angularVelocity = Vector3.zero; 
    
   // Record initial variables 
   if (Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) { 
      originalRotation = transform.rotation; 
      dir = hit.point - transform.position; 
      offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir)); 
      Spin (dir); 
   } 
} 


function Spin (dir : Vector3) { 
   var hit : RaycastHit; 
   var previousDirList : Array = new Array (); 
   var currentDir : Vector3; 
    
   // Initialize previous dir list 
   for (var i : int = 0; i < numberAverages; i++) { 
      previousDirList.Add (currentDir); 
   } 
    
   currentDir = dir; 
    
   // Make the object rotate with the cursor while we are grabbing it 
   while (Input.GetButton ("Fire1")  Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) { 
      // Remove first element of the array 
      previousDirList.RemoveAt (0); 
      // Add current dir to the end 
      previousDirList.Add (currentDir); 
      currentDir = hit.point - transform.position; 
      transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation; 
      yield; 
   } 
    
   // User let go of the mouse so make the object spin on its own 
   var avgPreviousDir : Vector3 = Vector3.zero; 
   for (dir in previousDirList) { 
      avgPreviousDir += dir; 
   } 
   avgPreviousDir /= numberAverages; 
   Kick (currentDir, avgPreviousDir); 
} 


function Kick (r2 : Vector3, r1 : Vector3) { 
   var linearVelocity : Vector3; 
   var angVelocity : Vector3; 
    
   // Calculate the angular velocity:  omega = r x v / r^2 
   linearVelocity = (r2 - r1) / Time.deltaTime; 
   rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude; 

}

There’s a camera property available in MonoBehaviour, so instead of Camera.main,ScreenPointToRay, you should just be able to use camera.ScreenPointToRay, as long as the script is attached to the camera object that is controlled by it.

Unfortunately the script is attached to the compass object and not to the camera. I’m desperately looking for a workaround for this. I have placed the compass in the field view of the Main Camera, so it’s at the bottom corner, and it works, but of course as soon as the camera moves the compass disappears from view.

The trouble is that I can’t have the compass inherit transform from another object because it controls the rotation of the scene (I use the compass for rotating the scene).

That’s why I tried putting the compass to be viewed by the another camera, because this way it would always be in the same position. But this way I can’t interact with it…

Cheers

How about creating variables of type Camera in the compass script and dragging your cameras to them? Then, you can use cam1.camera.ScreenPointToRay, etc.

Thanks Andeeee

I’ll give that a shot. Fingers crossed!

You are a life saver Andeee!
It works brilliantly.
Thank you, thank you!