- Home /
Raycasting Weapons with two cameras?
Thank you in advance to anyone reading this who can help! I am trying to make a "Battlemech" game in which the upper body of the mech will rotate independently of the torso and legs. The only way I could manage to do that without invoking the clumsiness of the the Self-relative WASD controls in Unity's Third Person Controller was to have a two-camera character rig. The current setup is with the Main Camera parented directly to the Third Person Controller and the secondary camera through the upper body which contains mouse rotation scripts and is also parented through the rest of the model to the Third Person Controller. I've noticed that when trying now to use ray-casting it's a "pick one or the other" scenario. In one case, if the upper body cam is the tagged the Main Camera I end up with an ambiguous sense of turning that rotates the rest of the model in step with the upper body thanks to the 3PC. And if I tag the Torso and leg camera as the Main Camera, all ray-casting happens in the direction of the legs and torso instead of the independently rotating upper body. My work around for this was somewhat of a gamble, I would choose to keep the Main camera with the legs and torso and only call upon the other camera when the player left-clicked to "fire a weapon" having the cameras alternate fast enough, so as to have the steering issue not be of any noticable detriment to the player. I wrote this script and it compiled, but when I run it in game, the two cameras flash back and forth in a sort of feedback loop, and when I cast the ray it either gets rid of both camera entirely or wont fire again... Anyone's help is greatly appreciated, whether it's a fix to my current issue, or a cleaner way of going about this, I'm all ears... #pragma strict
public var Effect : Transform;
public var TheDamage = 100;
public var Cam1 : Camera;
public var Cam2 : Camera;
function Start()
{
Cam1.enabled = true;
Cam2.enabled = false;
}
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
Cam1.enabled = !Cam1.enabled;
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
else (Input.GetMouseButtonDown(0));
{
Cam2.enabled = !Cam2.enabled;
}
}
I'm not sure what you are trying to do with your else statement
else (Input.Get$$anonymous$$ouseButtonDown(0));
{
Cam2.enabled = !Cam2.enabled;
}
But it worries me that the else line ends with a semi colon (;) which makes the code evaluate the condition then continue to the next line regardless of the truth of the condition. In addition the else statement uses the same condition as the if statement which makes it seem like you don't want the else statement to ever be called.
First of all Camera.ScreenPointToRay does not need to be called using Camera.main.ScreenPointToRay. In your case you can use Cam1.ScreenPointToRay() or Cam2.ScreenPointToRay().
Answer by NanjaKilo · Mar 27, 2015 at 04:41 PM
Thanks So much! Its fixed now! The reason I wanted to have the else there was so that in all other cases of the left mouse button being down it would still refer to the 1st lower body camera, that way whenever the layer fired a weapon there was a 100% of it returning to the original setup. with your feedback I saw that it was redundant. I kept the two cam variables. replaced camera.main. with Cam2, gave cam 2 a different tag to prevent conflicts, and deleted the else statement and Function Start. Now everything works swimmingly, thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613190819im_/https://answers.unity.com/themes/thub/images/avi.jpg)