sajjadi sajjadi - 1 month ago 20
Javascript Question

Switching between first person and third person(unity)

I have a project in unity.
How can change third person character to first person when enter on trigger?
I try this js code on player but don't work

#pragma strict

var ThePlayer:GameObject;
var FirstPersonCam:GameObject;
var ThirdPersonCam:GameObject;
var check;

function Start () {
ThirdPersonCam.gameObject.active=true;
FirstPersonCam.gameObject.active=false;
check=true;
}


function OnTriggerEnter( other :Collider)
{

if(other.gameObject.name == "rubbishTrigger1")
{
if(check){
FirstPersonCam.gameObject.active=true;
ThirdPersonCam.gameObject.active=false;
}
else{
FirstPersonCam.gameObject.active=false;
ThirdPersonCam.gameObject.active=true;
}
check=!check;
}

}


Please help me.
Thanks

Answer

I found my answer.below code is true and it work good.

#pragma strict

var ThePlayer:GameObject;
var player:GameObject;
var FirstPersonCam:GameObject;
var ThirdPersonCam:GameObject;
var trigger:GameObject;


function OnTriggerEnter (other : Collider) {

    if(FirstPersonCam.gameObject.active==false && ThirdPersonCam.gameObject.active==true){
        FirstPersonCam.gameObject.active=true;
        ThirdPersonCam.gameObject.active=false;
        player.gameObject.SetActive(false);
    }
}