Hi,
I was modifying the FPS tutorial and adding some “hot areas” that when a user stepped on them it would cut to a certain Camera. In the FPS tutorial I have the Main Cameras under the First Person Controller hierarcy. These fixed cameras are outside the hiearcy so they dont move with the players.
The problem is it seems to create a slow down and when I try to implement the hit areas my camera never responds. Here’s a small sample of what I have:
var firstPersonCamera : Camera;
var otherCamera : Camera;
function Start(){
firstPersonCamera.enabled = true;
otherCamera.enabled = false;
}
function OnControllerColliderHit(hit : ControllerColliderHit){
var collisionObj = hit.collider.gameObject.name ;
///////////Debug.Log("character collided with " + collisionObj );
if (collisionObj == "wall")
{
hitWall = true;
}
if (collisionObj != "workArea"){
firstPersonCamera.enabled = true;
otherCamera.enabled = false;
}else{
firstPersonCamera.enabled = false;
otherCamera.enabled = true;
}
}
function Update ()
{
if (taskCompletionRate == 55){
firstPersonCamera.enabled = false;
otherCamera.enabled = true;
Debug.Log("progres is still true");
}
What do you all think? Am I even closee to something? Thanks in advance.
PS: The “otherCamera” is activated if it is placed inside the FPS Controller Hierarcy but I dont want the camera changing position with the player.
OnControllerColliderHit() is probably not the way to do this, as it fires when you hit the floor as well as workArea. I suggest you use a trigger collider which encompasses the workArea and has something like this attached:
var playerCamera : Camera;
var otherCamera : Camera;
function OnTriggerEnter(other : Collider) {
if (other.tag == "Player") {
playerCamera.enabled = false;
otherCamera.enabled = true;
}
}
function OnTriggerExit(other : Collider) {
if (other.tag == "Player") {
playerCamera.enabled = true;
otherCamera.enabled = false;
}
}
The trigger zone only needs a Collider and this script attached, but make sure that the isTrigger checkbox is checked in its Collider component. Finally, make sure the GameObject with the player’s Collider (probably a CharacterController) is tagger “Player”.
Thanks for the help! This seems very logical to me. However, I followed your instructions and it appears that the trigger still does not react to anything. The provided script is attached to my object, trigger checked…and my FPContoller is tagged player.
Any ideas? Thanks though for the great start!
The GameObject “other1” is never read by your trigger functions, because you’ve called the passed Collider “other1” as well. A function will always look at for locally declared variables before ones that are declared outside the function. I’m not sure why this script would work differently, even for one frame.
Do you have any other scripts attached to objects in your scene which may be enabling/disabling cameras?
Where? The script ddurieux posted has three variables named other1. The arguments to the functions are obscuring the GameObject declared outside. The variables inside the functions should have different names from the ones outside.
It’s not, but you don’t need to do what I think you’re trying to do anyway. OnTriggerEnter will always pass a reference to the collider that entered the trigger. The issue with calling it other1 is that there is already a variable called other1, which is generally a bad idea and definitely doesn’t help in this case.