- Home /
enter a trigger to access other camera?
Hi there,
I'm currently new to unity and trying to understand this problem, i currently have an fps camera and when i walk into a trigger i want it to switch to a another camera, which is where i want to be shooting from.
I don't understand how i would put this into javascript, I understand on trigger enter and such, i would also like to be able to click my right mouse button to activate the camera?
Any help would be much appreciative.
Comment
Best Answer
Answer by Cherno · Oct 16, 2013 at 05:41 PM
var PlayerCamera : Camera;//drag your main camera gameobject into this field in the inspector
var GunCamera : Camera;//drag your gun camera gameobject into this field in the inspector
var InGunCameraZone : boolean;//don't touch :)
function Update()
{
if(Input.GetMouseButtonDown(1))//if the right mouse button (button 1) is pressed down...
{
if(InGunCameraZone == true)//... and a rigidbody is touching this object's collider...
{
PlayerCamera.enabled = false;//disable the main camera
GunCamera.enabled = true;//enable the gun camera
}
else//if no rigidbody is touching this object's collider...
{
PlayerCamera.enabled = true;//... switch the camera back again
GunCamera.enabled = false;
}
}
}
function OnTriggerEnter()
{
InGunCameraZone = true;
}
function OnTriggerExit()
{
InGunCameraZone = false;
}
Cheers. In that case, please mak the answer as accepted :)