Camera Switcher works... Kinda.

I have a Camera switcher that is supposed to switch Cameras and Player Controls at the press of a button. It works but, the problem is, it’s supposed to disable the switching unless your ship is within a specific collider with a specific tag. Not only does the switcher work everywhere (I can go to Earth straight from Mars) but the Player Controls on one of the Planets does not have vertical look working! I need for everything to be disabled unless within a certain Planet’s Collider and then allot you then to switch the planet Controller disabling the Ship’s controls. Can anybody help me with my issue? I appreciate any help that you may have to offer!

Thanks,

Brady, the Developer in Need!

Scripts:

Camera Switcher:

var SpaceshipCamera : GameObject;
var EarthCamera : GameObject;
var MarsCamera : GameObject;
var MarsPlayer : GameObject;
var exitPoint : GameObject;
var MarsExitPoint : GameObject;
var EarthPlayer : GameObject;
var canExit : boolean;
var MarsCanExit : boolean;
var moveSpace : SpaceshipControls;


function Start(){
    moveSpace = this.GetComponent(SpaceshipControls);
}

function Update()
{
    if(Input.GetKeyDown("1"))
    {  
        GetComponent(SpaceshipControls).enabled = true;  
        SpaceshipCamera.active = true;
        EarthCamera.active = false;
	    MarsCamera.active = false;
		MarsPlayer.active = false;
        EarthPlayer.active = false;
        moveSpace.enabled = true;
    }
    if(Input.GetKeyDown("2") && canExit == true)
    {  
        GetComponent(SpaceshipControls).enabled = false;
        SpaceshipCamera.active = false;
        EarthCamera.active = true;
		MarsCamera.active = false;
        EarthPlayer.transform.position.x = exitPoint.transform.position.x;
        EarthPlayer.transform.position.y = exitPoint.transform.position.y;
        EarthPlayer.transform.position.z = exitPoint.transform.position.z;
        EarthPlayer.active = true;
        moveSpace.enabled = false;
		MarsCanExit = false;
		}
    if(Input.GetKeyDown("3") && MarsCanExit == true)
    {  
        GetComponent(SpaceshipControls).enabled = false;
        SpaceshipCamera.active = false;
		EarthCamera.active = false;
		EarthPlayer.active = false;
        MarsCamera.active = true;
        MarsPlayer.transform.position.x = MarsExitPoint.transform.position.x;
        MarsPlayer.transform.position.y = MarsExitPoint.transform.position.y;
        MarsPlayer.transform.position.z = MarsExitPoint.transform.position.z;
        MarsPlayer.active = true;
        moveSpace.enabled = false;
		canExit = false;
    }
}

function OnTriggerStay(Col : Collider)
{
    if(Col.tag == "EarthAtmosphere"){
        canExit = false;
    }
	else
	{
        canExit = true;
    }
	if(Col.tag == "MarsAtmosphere"){
        MarsCanExit = false;
    }
	else
	{
        MarsCanExit = true;
    }
}

Mars Controls:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (MarsGravityBody))]
public class MarsFirstPersonController : MonoBehaviour {
	
	public float mouseSensitivityX = 250;
	public float mouseSensitivityY = 250;
	public float walkSpeed = 6;
	public float jumpForce = 220;
	public LayerMask groundedMask;
	
	bool grounded;
	Vector3 moveAmount;
	Vector3 smoothMoveVelocity;
	float verticalLookRotation;
	Transform cameraTransform;
	
	void Awake() {
		Screen.lockCursor = true;
		cameraTransform = Camera.main.transform;
	}
	
	void Update() {
		transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
		verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
		verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
		cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
		
		float inputX = Input.GetAxisRaw("Horizontal");
		float inputY = Input.GetAxisRaw("Vertical");
		
		Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
		Vector3 targetMoveAmount = moveDir * walkSpeed;
		moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
		
		if (Input.GetButtonDown("Jump")) {
			if (grounded) {
				rigidbody.AddForce(transform.up * jumpForce);
			}
		}
		if (Input.GetKey(KeyCode.LeftShift)) {
			if (grounded) {
				rigidbody.AddRelativeForce(0, 0, 2 * walkSpeed);
			}
		}
		Ray ray = new Ray(transform.position, -transform.up);
		RaycastHit hit;
		
		if (Physics.Raycast(ray, out hit, .5f, groundedMask)) {
			grounded = true;
		}
		else {
			grounded = false;
		}
		
	}
	
	void FixedUpdate() {
		Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
		rigidbody.MovePosition(rigidbody.position + localMove);
	}
}

Earth Controls:

using UnityEngine;
using System.Collections;
 
[RequireComponent (typeof (GravityBody))]
public class FirstPersonController : MonoBehaviour {

        public float mouseSensitivityX = 250;
        public float mouseSensitivityY = 250;
        public float walkSpeed = 6;
        public float jumpForce = 220;
        public LayerMask groundedMask;

        bool grounded;
        Vector3 moveAmount;
        Vector3 smoothMoveVelocity;
        float verticalLookRotation;
        Transform cameraTransform;
		
        void Awake() {
                Screen.lockCursor = true;
                cameraTransform = Camera.main.transform;
        }
		
        void Update() {
                transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
                verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
                verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
                cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;

                float inputX = Input.GetAxisRaw("Horizontal");
                float inputY = Input.GetAxisRaw("Vertical");
 
                Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
                Vector3 targetMoveAmount = moveDir * walkSpeed;
                moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);

                if (Input.GetButtonDown("Jump")) {
                        if (grounded) {
                                rigidbody.AddForce(transform.up * jumpForce);
                        }
                }
		        if (Input.GetKey(KeyCode.LeftShift)) {
			            if (grounded) {
				                rigidbody.AddRelativeForce(0, 0, 2 * walkSpeed);
			            }
		        }
                Ray ray = new Ray(transform.position, -transform.up);
                RaycastHit hit;
 
                if (Physics.Raycast(ray, out hit, .5f, groundedMask)) {
                        grounded = true;
                }
                else {
                        grounded = false;
                }
 
        }
		
        void FixedUpdate() {
                Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
                rigidbody.MovePosition(rigidbody.position + localMove);
        }
}

Spaceship Controls:

var turnspeed = 5.0;
var speed = 5.0;
private var trueSpeed = 0.0;
var strafeSpeed = 5.0;
var MarsCameraTrigger : Collider;
var EarthCameraTrigger : Collider;

function Update () {

	var roll = Input.GetAxis("Roll");
	var pitch = Input.GetAxis("Pitch");
	var yaw = Input.GetAxis("Yaw");
	var strafe = Vector3(Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime, Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime, 0);

	var power = Input.GetAxis("Power");

	//Truespeed controls

	if (trueSpeed < 10 && trueSpeed > -3){
	trueSpeed += power;
	}
	if (trueSpeed > 10){
	trueSpeed = 9.99;	
	}
	if (trueSpeed < -3){
	trueSpeed = -2.99;	
	}
	if(Input.GetKey(KeyCode.W)){
	    rigidbody.AddRelativeForce(0, 0, speed);
	}	
	if(Input.GetKey(KeyCode.S)){
	    rigidbody.AddRelativeForce(0, 0, -speed);
	}	
	if(Input.GetKey(KeyCode.A)){
	    rigidbody.AddRelativeForce(-speed /2, 0, 0);
	}	
	if(Input.GetKey(KeyCode.D)){
        rigidbody.AddRelativeForce(speed /2, 0, 0);
	}
	if(Input.GetKey(KeyCode.LeftShift)){
	    rigidbody.AddRelativeForce(0, 0, 2 * speed);
	}	
	rigidbody.AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, roll*turnspeed*Time.deltaTime);
	   
}

Looking at that script the only time you CAN’T exit is if you’re in the EarthAmosphere collider, that seems the wrong way round to me. I could have read it wrong but is it what you’re trying for.

And as a simple test why not swap the true and false values in OnTriggerStay to see if that fixes the problem.

So your camera swapping issue seems to be in the OnTriggerStay function you are checking for atmosphere tags then setting things to true if the tag fails. So if the tags were to fail all the time they would be true all the time so you would be able to swap all the time. I’m guessing you have your true/false logic there flipped by accident. However, since it works everywhere the if(Col.tag == "EarthAtmosphere") is probably also having an issue. You might not be checking the tag properly or if there are multiple colliders it may only be checking the first. Col can return multiple colliders, I believe, so check for that and iterate through them. But also, using OnTriggerStay is likely a mistake since once you exit the collider it won’t trigger again to disable the switch option. So think about using OnTriggerEnter to set the appropriate canExit to true and OnTriggerExit to set it to false.

I didn’t think I was going to solve the vertical look issue, but then I stumbled into it in the code anyways. In the FirstPersonController scripts in the Awake line we can see cameraTransform = Camera.main.transform; this is run at start up and not updated so both controllers are set to change the same camera(vertical only). You can fix this by removing that line and changing the cameraTransform variable to public then setting it manually in the inspector. Or if you are Enabling and Disabling the game objects, you might be able to just change Awake() to OnEnable(), but that also relies on you changing what the main camera is defined as.

Hope that helps!