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);
}