Search Unity

Camera Cuts (to another) when hitting an area

Discussion in 'Scripting' started by ddurieux, Jul 19, 2007.

  1. ddurieux

    ddurieux

    Joined:
    Mar 1, 2007
    Posts:
    33
    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:

    Code (csharp):
    1. var firstPersonCamera : Camera;
    2. var otherCamera : Camera;
    3.  
    4. function Start(){
    5.    
    6.         firstPersonCamera.enabled = true;  
    7.     otherCamera.enabled = false;
    8. }
    9.  
    10. function OnControllerColliderHit(hit : ControllerColliderHit){
    11.     var collisionObj = hit.collider.gameObject.name ;
    12.     ///////////Debug.Log("character collided with " + collisionObj );
    13.            
    14.     if (collisionObj == "wall")
    15.     {          
    16.         hitWall = true;
    17.     }
    18.    
    19.     if (collisionObj != "workArea"){
    20.         firstPersonCamera.enabled = true;
    21.         otherCamera.enabled = false;
    22.     }else{
    23.  
    24.         firstPersonCamera.enabled = false;
    25.         otherCamera.enabled = true;
    26. }
    27. }
    28.  
    29. function Update ()
    30. {
    31. if (taskCompletionRate == 55){
    32.  
    33.             firstPersonCamera.enabled = false;
    34.         otherCamera.enabled = true;
    35.            
    36.             Debug.Log("progres is still true");
    37.         }
    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.
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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:

    Code (csharp):
    1. var playerCamera : Camera;
    2. var otherCamera : Camera;
    3.  
    4. function OnTriggerEnter(other : Collider) {
    5.     if (other.tag == "Player") {
    6.         playerCamera.enabled = false;
    7.         otherCamera.enabled = true;
    8.     }
    9. }
    10.  
    11. function OnTriggerExit(other : Collider) {
    12.     if (other.tag == "Player") {
    13.         playerCamera.enabled = true;
    14.         otherCamera.enabled = false;
    15.     }
    16. }
    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".
     
  3. ddurieux

    ddurieux

    Joined:
    Mar 1, 2007
    Posts:
    33
    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!
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I'm not sure what the problem is. Try adding the following two lines to the script to see whether the entry and exit are registering at all:
    Code (csharp):
    1. var playerCamera : Camera;
    2. var otherCamera : Camera;
    3.  
    4. function OnTriggerEnter(other : Collider) {
    5.    Debug.Log(other.name + " entered");
    6.    if (other.tag == "Player") {
    7.       playerCamera.enabled = false;
    8.       otherCamera.enabled = true;
    9.    }
    10. }
    11.  
    12. function OnTriggerExit(other : Collider) {
    13.    Debug.Log(other.name + " exited");
    14.    if (other.tag == "Player") {
    15.       playerCamera.enabled = true;
    16.       otherCamera.enabled = false;
    17.    }
    18. }
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Just a quick thought, does the player object have colliders and physics on it?
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Although not strictly speaking Rigidbodies, CharacterControllers don't need an attached Rigidbody to register collisions with other Colliders.

    Does the above script give you any entry/exit messages, ddurieux?
     
  7. ddurieux

    ddurieux

    Joined:
    Mar 1, 2007
    Posts:
    33
    No, it appears nothing passes at all. They pass if I add

    Code (csharp):
    1. var other1 : GameObject;
    2.  
    3. function OnTriggerEnter(other1 : Collider) {
    4.    Debug.Log(other1.name + " entered");
    5.    if (other1.tag == "Player") {
    6.       playerCamera.enabled = false;
    7.       otherCamera.enabled = true;
    8.    }
    9. }
    10.  
    11. function OnTriggerExit(other1 : Collider) {
    12.    Debug.Log(other1.name + " exited");
    13.    if (other1.tag == "Player") {
    14.       playerCamera.enabled = true;
    15.       otherCamera.enabled = false;
    16.    }
    17. }
    Then I pass entered exited the camera reacts for 1 frame only...even when I add an update function. Any ideas...and thanks so far!
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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?
     
  9. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    wouldn't other1.collider fix it?
     
  10. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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.
     
  11. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    Code (csharp):
    1. function OnTriggerEnter(other1.collider : Collider)
    is what i meant. not sure if that's valid syntax.
     
  12. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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.
     
  13. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    doh! yes indeed! :wink: