- Home /
Question by
Jshere · May 09, 2016 at 09:53 AM ·
c#camerascripting problemtransform.positiontransparency
Unity Camera and transform?
Hi All
I Have my plane script, a plane and multiple cameras with in my scene. What I am looking to do is when "V" is pressed it changes to the second camera and so on. I have the camera change working properly but when it disables the main chase camera it stops the plane from moving forward. My Plane script is in C# and the camera change script is in Java.
Plane script
using UnityEngine;
using System.Collections;
public class PlanePilot : MonoBehaviour {
public float turnSpeed = 50.0f;
public float speed = 80.0f;
public GameObject explosion;
void Start () {
Debug.Log("Plane Pilot script added to: " + gameObject.name);
}
// Update is called once per frame
void Update () {
Vector3 moveCamTo = transform.position - transform.forward * 15.0f + Vector3.up * 5.0f;
float bias = 0.96f;
Camera.main.transform.position = Camera.main.transform.position * bias +
moveCamTo * (1.0f-bias);
Camera.main.transform.LookAt (transform.position + transform.forward * 20.0f );
transform.position += transform.forward * Time.deltaTime * speed;
speed -= transform.forward.y * Time.deltaTime * 50.0f;
if (speed < 35.0f) {
speed = 35.0f;
}
if (speed > 90.0f) {
speed = 90.0f;
}
transform.Rotate( Input.GetAxis("Vertical"), 0.0f, -Input.GetAxis("Horizontal") );
if(Input.GetKey(KeyCode.PageUp))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.PageDown))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.Q))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.E))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
float terrainHightWhereWeAre = Terrain.activeTerrain.SampleHeight (transform.position);
if (terrainHightWhereWeAre > transform.position.y)
{
Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
}
Cam script
var cam1: Camera;
var cam2: Camera;
var cam3: Camera;
function Start() {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
Void update ()
if (Input.GetKeyDown(KeyCode.V) && (cam1.enabled == true)) {
cam1.enabled = false;
cam2.enabled = true;
cam3.enabled = false;
}
else if (Input.GetKeyDown(KeyCode.V) && (cam2.enabled == true)) {
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = true;
}
else if (Input.GetKeyDown(KeyCode.V) && (cam2.enabled == true)) {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
}
Thanks
Comment
Answer by aditya · May 09, 2016 at 10:15 AM
In your cam script the last elseif
is again checking for cam2.enabled
which you have already disabled in second last elseif