- Home /
Script that switches between first and third person controller
Hello - in my game I switch between first and third person. When the third person collides with a certain trigger - the third person controller is destroyed, and a new one is instantiated.
The problem I have is that once the new controller is instantiated, this script no longer works - switching between first and third person. (as of course I only assigned the first third person controller and camera in the hierarchy)
Is there a way of changing the script so that rather than assigning the third person controller and camera in the hierarchy, the script just references whichever third person controller is currently active?
(Sorry I'm not sure how exactly to phrase it - I'm still very new to scripting)
This is the script I'm using to switch between characters (JavaScript):
#pragma strict
var cam01 : GameObject; // first person camera
var cam02 : GameObject; // third person camera
var player01 : GameObject; //first person controller
var player02 : GameObject; //third person controller
var check; // New check-variable
//start with first person active
function Start() {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player02.active = false;
check = true;
}
function Update() {
player01.transform.position = player02.transform.position;
if (Input.GetKeyDown ("return")) {
if(check) {
cam01.gameObject.active = false;
cam02.gameObject.active = true;
player01.active = false;
player02.active = true;
}
else {
cam01.gameObject.active = true;
cam02.gameObject.active = false;
player01.active = true;
player02.active = false;
}
check = !check;
}
Thanks very much, Laurien
You could assign the player02 variable after you instantiate it.
Pseudo Code:
var newthirdpersoncontroller = Instantiate(....);
var script : TheScript = playerGO.GetComponent(TheScript);
script.player02 = newthirdpersoncontroller;
No problem. I forgot. you also have to assign the cam02 variable when you instantiate the new one.
So you also have to do:
script.cam02 = CameraofnewPrefab; //CameraofnewPrefab should be assigned with the Camera of the new ThirdPersonController if there is one.
Is it possible that in the edge of the terrain there is a active Camera? I'm guessing that in the time between destroying the old Camera and instantiating the new one it automatically changes to the next active camera.
I'm gonna convert one of my comments to an answer so that you can accept it to mark the question as solved if it is.
That sounds weird. There has to be a camera there either a static one or it's instantiating one. $$anonymous$$aybe it's the third person/First Person camera? Could you check your scene hirarchy/scene view when this happens to see which camera or if there is a camera where the game view points from.
To which Gameobject is the exception pointing?
I'm guessing that the vincentCamera is the camera in the corner. You're instantiating this gameobject wihtout setting position or rotation. This way it will be instantiated, i think, at (0,0,0).
You could try to add the Camera already to the prefab and then ins$$anonymous$$d of instantiating it just root to it using transform.Find("PlayerCamera") or something like this.
Or you could instantiate the camera and also set the position and rotation.
Answer by ExTheSea · Jun 02, 2013 at 01:56 PM
Not really like this you would do it more like this:
var prefab : Transform;
var script : SwitchCharacters;
var playerGO : GameObject; //<--Should be filled with the Gameobject the SwitchCharacters-Script is attached to.
private var hasPlayed = false;
function OnTriggerEnter () {
var pos : Vector3;
if (!hasPlayed){
var newprefab = Instantiate (prefab);
script = playerGO.GetComponent(SwitchCharacters);
script.player02 = newprefab;
hasPlayed = true;
}
}
Depending on where the instantiating script is attached to you can maybe replace the playerGO with something like transform, transform.parent, .... Whatever works in your case.
Answer by LostInCode404 · Jun 02, 2013 at 03:57 PM
I you want to switch between tps and fps. Create a static var fps and set its bool value to true. Now whatever you want to do in fps, do it in a if condition if fps is true and else if it is false do what you want to do in fps. If the trigger event happens, check tha value of var fps. If its true set it to false, else it is false, set it to true. Hope u understood. I will get u the script in a few days.
Please read the question first. The switching is working already but she has a problem when instantiating a new thirdpersoncontroller.
Also you should avoid using static variables if possible.
Answer by DubstepDragon · Jun 05, 2013 at 04:32 PM
This is very easy. All you need is a 3rd person controller with the camera where the head is, so as to look first person, and just move the camera back however you want: If you want to move it instantly, so it switches right away; if you want to scroll out to zoom out. You can just set up some sort of line that the camera moves on.
I am currently working on this myself and have it jumping between two spots and am about to start working on a smooth transition between the two here is the code it works off the left mouse button but that can be changed with ease.
using UnityEngine;
using System.Collections;
public class CameraControl : $$anonymous$$onoBehaviour {
public Transform myTransform;
public Transform target;
private float offsetY = 1.5f;
private float offsetZ = -6.0f;
public Vector3 maxDistance;
public Vector3 $$anonymous$$Distance;
public Vector3 curDistance;
public bool cameraSetAt;
public float smoothingSpeed = 1.0f;
private float smoothingVelocity;
void Awake(){
target = GameObject.FindWithTag("Player").GetComponent<Transform>();
myTransform = transform;
}
// Use this for initialization
void Start () {
cameraSetAt = true;
curDistance = new Vector3 (target.position.x, target.position.y + offsetY, target.position.z + offsetZ);
}
// Update is called once per frame
void Update () {
maxDistance = new Vector3 (target.position.x, target.position.y + offsetY, target.position.z + offsetZ);
$$anonymous$$Distance = new Vector3 (target.position.x, target.position.y, target.position.z);
// Changes the cameras current position
if(Input.GetButtonDown("Fire1")){
if(cameraSetAt){
cameraSetAt = false;
Debug.Log("false");
}else{
cameraSetAt = true;
Debug.Log("true");
}
}
}
void LateUpdate(){
if(cameraSetAt)
myTransform.position = maxDistance;
if(!cameraSetAt)
myTransform.position = $$anonymous$$Distance;
}
}
Your code is very interesting and peculiar... I had the impression that this works too:
using UnityEngine;
using System.Collections;
public class $$anonymous$$odifiedGravitronThingy : $$anonymous$$onoBehaviour {
Vector3 gravity;
void Start() {
gravity = Physics.gravity;
}
void Update() {
Physics.gravity = gravity;
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) {
gravity.x = 0;
gravity.y = 2;
gravity.z = 0;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) {
gravity.x = 0;
gravity.y = 0;
gravity.z = 2;
}
}
}
However it stops after the RigidBody lies still... :/