- Home /
Switching Several Cameras(HorrorGame)
Hey guys, my question should be simple hopefully. I have a level done, with 5 cameras set up at different positions and angles. The best way to describe what I am going for would be like how the camera switches nice and clean like in older horror games. Silent hill for example with all the amazing angels.
So right now, I have the 5 cameras with 5 boxes for triggers to be touched by the player. i know triggers and such are needed, but im not sure on how to write code just yet. C# or Java doesn't matter. Either seems fine. Any ideas? Seems simple but I just don't know where to start!
searched other questions but i cant find any that deal with multiple cameras switching by triggers.
Other advice is welcome! whatever it takes to get it done.
Check out my profile icon! thats my character for the game, hopefully it will inspire someone to help!
Answer by AlucardJay · Nov 07, 2012 at 06:33 AM
So I have been thinking about this, and may have a flexible approach.
Each camera is a component or child of your trigger object. So tell the camera to enable when the trigger is entered. But then there is the problem of turning the old camera off. Just store a reference to the currently used camera, and when the trigger enables a new camera, then disable the stored one, then make sure to store a reference to the new current camera.
Here is some rough untested code to try and explain the theory. The idea then is no matter how many cameras there are, this should keep working (as long as the trigger volumes are not too close together). Adding or removing cameras and trigger volumes becomes fun, not a hassle with rewriting code =]
// -- PLAYER SCRIPT --
#pragma strict
// FOR MULTIPLE CAMERAS
public var currentCam : Camera;
function Start()
{
// Enable the first camera
currentCam = Camera.allCameras[0];
// disable all other cameras
for ( var i:int = 1; i < Camera.allCameras.Length; i ++ )
{
Camera.allCameras[i].enabled = false;
}
}
// ----
// -- TRIGGER SCRIPT --
#pragma strict
var thePlayerScript : PlayerScript;
var myCamera : Camera;
function Start()
{
// find myCamera (child or component of trigger object)
myCamera = transform.parent.FindChild( "Camera" ).GetComponent( Camera );
// find the Player, store reference to player script
// to use .enabled = false; when switching cameras
thePlayerScript = GameObject.FindWithTag( "Player" ).GetComponent( PlayerScript );
}
function OnTriggerEnter( other : Collider )
{
// disable current camera
thePlayerScript.currentCam.enabled = false;
// enable this camera
myCamera.enabled = true;
// assign this camera as the current camera
thePlayerScript.currentCam = myCamera;
Debug.Log( transform.parent.gameObject.name + " Triggered by " + other.gameObject.name );
}
Any feedback is most welcome =]
The character looks great ! But I was inspired by being re$$anonymous$$ded of Resident Evil 3 =]
I don't have lerpz, isn't that a 2D tutorial?
If you can take a screenshot of the scene, with the character highlighted and the inspector showing, and edit your question, post the picture so I can see what the lerpz character is. Or just clarify the name of the tutorial so I can download the lerpz character and check it out. It should be no problem to make work, maybe some fancy swapping of tags, or just assigning currentCam to the lerpz script. Pass on the extra info, did you just use my code as it is? wow it worked!
Edit : Just looking at the scripts involved in the 2D Lerpz and the camera target stuff, it seems next to impossible to just mash it together with a camera system like you have designed.
The theory works : http://www.alucardj.net16.net/unityanswers/ResEvilCameras.html
As I said above, the problem will be making a hybrid of the Lerpz camera system and your multiple camera system.
The NEW update to your script works perfectly!! Amazing! Thanks alot man Il give it thumbs up now :)
Edit: The cameras do indeed switch, BUT I dont think they turn off, while looking at the inspector, it shows the all cameras still being on, and there is a strong leg as you move from camera to camera cause of it, what could it beee H$$anonymous$$$$anonymous$$$$anonymous$$ lol
Thanks for the comments =]
The controls are so basic they shouldn't be called controls ! It was just to have something that hit the triggers. But this is how Resident Evil worked, your controller forward moved the character forward to how the character is facing, not how the camera is facing. SO if you were walking forward away from a camera then the camera swapped, pusing forward would still walk forward so the character would move towards the new camera. For controls that move the charater the way the camera is facing, I did an answer here : http://answers.unity3d.com/questions/318305/character-controls-like-the-game-fable.html
Thanks, I like to think I'm legit, but ama self-taught noob and only a programmer, not an artist. I like to help people too. That Ghosthunter game I did in Feburary, 1 month after starting Unity. The music player is broken so every restart there is an extra music player. I actually removed it but someone asked me to put it back, so it is there to show what can be done by a new user =] I do want to fix it, but with all I have learned I am simply going to rewrite it. I am proud that all the objects are Unity natives, even the turkeys!
The image you sent looks good, the game should turn out neat. But unfortunately without seeing the trigger volumes (like i did with the transparent red material) I cannot see the volumes, the area they affect or more importantly if they are overlapping or too close.
I found the 3D tutorial (thanks for the link, i was thinking of lerps here : http://unity3d.com/support/resources/tutorials/2d-gameplay-tutorial), so am about to unzip and look at the character controller.
WIth your issue with more than one camera, I actually wrote a Start function on the player to disable all cameras and then just turn the first one on. Then as a trick, I put the character just above the triggere where the player starts, so on Play, character falls to the floor, activating the trigger and automatically setting the correct camera to be the first used camera. All other cameras have to be disabled, here is my function I used :
// FOR $$anonymous$$ULTIPLE CA$$anonymous$$ERAS -- ResEvil
public var currentCam : Camera;
function Start()
{
// Enable the first camera
currentCam = Camera.allCameras[0];
// disable all other cameras
for ( var i:int = 1; i < Camera.allCameras.Length; i ++ )
{
Camera.allCameras[i].enabled = false;
}
}
I don't know what else to suggest without looking at the 3D lerpz controller, but as in the demo the trigger volume multi camera system works (as long as there is only ever one camera enabled!). If I find anything out or make a demo project with the Lerpz char, I shall post back. Good Luck.
Your answer
Follow this Question
Related Questions
Multiple Camera.main.ScreenPointToRay 1 Answer
Add multiple cameras problems 1 Answer
Switching cameras with the mouse button 1 Answer
UI Buttons switch cameras 3 Answers
Main Camera Switch? 3 Answers