Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Switching camera on keypress

Discussion in 'Scripting' started by GrimWare, Jan 16, 2010.

  1. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    Hello,

    Currently working on a project where I find myself in a pickle and needing some help :?

    Attached is a little screenshot to help explain my situation.

    The player can walk up and down the deck of the ship, and I would like to be able to "man" the guns of the ship. This is kind of what I'm envisioning:

    Walk up to the back of one of the gun turrets and press "e" to enter. Upon hitting E, the view will change to a view of the gun turret's aiming sight, (which I have a .psd file with transparency for this) that takes up the whole screen. Pressing space or left mouse button fires the gun, and pressing E exits the turret.

    How do you suggest I do this? If you could provide some sample code that would be great as I'm more of a visual learner. I prefer Javascript as I own a copy of Will Goldstone's Unity Development book, but it doesn't cover anything like this.
     

    Attached Files:

  2. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    Okay, I've added a new camera at the first turret, but now whenever i start the application instead of being the first person controller camera it spawns this one instead. Of course I don't have any scripting yet to switch but while in this view I have designated the maximum and minimum values for rotation of the turret.
     

    Attached Files:

  3. h.istvan

    h.istvan

    Joined:
    Aug 4, 2009
    Posts:
    77
  4. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    This works unbelievably well! Thanks!

    And before, the turret itsself wouldn't turn with the camera as I had the camera parented to the turret. I believe by switching this around, the turret will rotate with the camera, attempting now 8)
     
  5. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    Okay, it worked, but not like I wanted. Whenever you move any of the cameras, it moves all of them instead of individually.

    Is it possible to disable the movement of a camera and also, in this case a turret, while viewing from a different camera?

    Here's my change camera script:

    Code (csharp):
    1. var camera1 : GameObject;
    2. var camera2 : GameObject;
    3. var camera3 : GameObject;
    4.  
    5. function Start () {
    6.    camera1.camera.enabled = true;
    7.    camera2.camera.enabled = false;
    8.    camera3.camera.enabled = false;
    9. }
    10.  
    11. function Update () {
    12.    if (Input.GetKeyDown ("2")){
    13.       camera1.camera.enabled = false;
    14.       camera3.camera.enabled = false;
    15.       camera2.camera.enabled = true;
    16.    }
    17.    if (Input.GetKeyDown ("1")){
    18.       camera1.camera.enabled = true;
    19.       camera2.camera.enabled = false;
    20.       camera3.camera.enabled = false;
    21.    }    
    22.    if (Input.GetKeyDown ("3")){
    23.    camera3.camera.enabled = true;
    24.    camera2.camera.enabled = false;
    25.    camera1.camera.enabled = false;
    26.     }
    27. }
     
  6. h.istvan

    h.istvan

    Joined:
    Aug 4, 2009
    Posts:
    77
    Maybe you need to make modifications in the script in which you're controlling the cameras. Try to tag the cameras and in the controller script find them by tag and try to control the appropriate camera.
     
  7. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hey man, that script is kind of what you want but i think that the issue you are seeing is due to your camera control scripts still being active- this is because with enable youare simply switching off the camera component of those objects, and leaving the object active so even though you aren't looking through those cameras you are still moving them. To avoid this try deactivating the whole object first, so instead of saying camera1.camera.enabled=false; try camera1.active=false.

    Hope that helps!

    Will
     
  8. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    Hey Will, cool that you replied having read your book :)

    It works as in it stops the rotation of the other cameras/turrets, but I cannot switch back to the original camera, or any other camera for that matter.

    For instance, I want to go to camera/turret1, I press "2" to switch to the camera and everything seems to be good, until I want to switch back to the original camera, nothing happens.
     
  9. wiredheat3

    wiredheat3

    Joined:
    Jan 20, 2013
    Posts:
    1
    var camera1 : Camera;
    var camera2 : Camera;

    function Start () {
    camera1.enabled = true;
    camera2.enabled = false;
    }

    function Update () {
    if (Input.GetKeyDown ("2")){
    camera1.enabled = false;
    camera2.enabled = true;
    camera1.active = false;
    camera2.active = true;
    }
    if (Input.GetKeyDown ("1")){
    camera1.enabled = true;
    camera2.enabled = false;
    camera1.active = true;
    camera2.active = false;
    }
    }
     
  10. N2KMaster

    N2KMaster

    Joined:
    Jul 15, 2013
    Posts:
    64
    The link above here is dead, I'm interested in this solution as well. Car racing, would like to be able to swap cameras around a car (ie sideviews, back and front) I already got cameras linked to the car properly following it, minimap's work got review and side view mirrors working as well for it. How do I do this up so those cameras are always with the car but can do the keypress swap, i was thinking something easy 1,2,3,4 cuz its 4 cameras but releasing the key sends it back to the main follow camera i got on it. Hope i worded that right, the gui end of this i almost got it figured out however this coding side of it is confusing me at times. And keep in mind most of its been on a guess for me and it this far and am loving my unity3d experience overall so far.
     

    Attached Files:

  11. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
  12. N2KMaster

    N2KMaster

    Joined:
    Jul 15, 2013
    Posts:
    64
    K this is weird and the second time I've seen them talking about a 2nd camera as a texture. I didn't have to make my cameras a texture for anything or go thru any these steps for the cameras. This is whats confusing me cuz i used a completely different method to get the minimap and rearviews working.

    Wrote that script, did what it told me. Got this, and tells me compiler has errors now cuz its missing a ; somewheres.......Great.....

    Assets/crossfade.js(1,18): UCE0001: ';' expected. Insert a semicolon at the end.

    Glad I saved it cuz using these instructions i couldn't even get rid of it.
     

    Attached Files:

    Last edited: Oct 20, 2014
  13. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    You should post your script.
     
  14. N2KMaster

    N2KMaster

    Joined:
    Jul 15, 2013
    Posts:
    64
    var cam1 : Camera-Minimap;
    var cam2 : Camera-Chase;
    var fadeTime : float = 2;
    private var inProgress = false;
    private var mat : Material;
    function Start () {
    mat = new Material (
    "Shader \"Transparent/Unlit\" {" +
    "Properties {" +
    " _Color (\"Main Color\", Color) = (1,1,1,1)" +
    "}" +
    "SubShader {" +
    " Pass {" +
    " ZTest Always ZWrite Off Cull Off Alphatest Greater 0 ColorMask RGB Lighting Off" +
    " Blend SrcAlpha OneMinusSrcAlpha" +
    " SetTexture [_RenderTex] {" +
    " constantColor [_Color]" +
    " Combine texture * constant, texture * constant" +
    " }" +
    " }" +
    "}" +
    "}"
    );
    if (GetComponent(AudioListener)) {
    DestroyImmediate(GetComponent(AudioListener));
    }
    camera.enabled = false;
    }
    function Update () {
    if (Input.GetKeyDown("C")) {ScreenCrossFade(fadeTime);}
    }
    function ScreenCrossFade(fadeTime : float) {
    if (inProgress) {return;}
    inProgress = true;

    // Make this render camera duplicate the currently active camera
    if (cam1.gameObject.active) {
    var cam = cam1;
    var otherCam = cam2;
    }
    else {
    cam = cam2;
    otherCam = cam1;
    }
    transform.position = cam.transform.position;
    transform.rotation = cam.transform.rotation;
    camera.CopyFrom(cam);
    camera.depth = otherCam.depth+1;

    // Switch cameras
    cam1.gameObject.active = !cam1.gameObject.active;
    cam2.gameObject.active = !cam2.gameObject.active;

    // Do the cross-fade
    camera.enabled = true;
    for (i = 1.0; i > 0.0; i -= Time.deltaTime / fadeTime) {
    mat.color.a = i;
    yield;
    }

    // Clean up
    camera.enabled = false;
    inProgress = false;
    }
    // Draw a quad with a rendertexture
    function OnRenderImage (source : RenderTexture, dest : RenderTexture) {
    RenderTexture.active = dest;
    source.SetGlobalShaderProperty ("_RenderTex");

    GL.PushMatrix ();
    GL.LoadOrtho ();

    mat.SetPass (0);
    GL.Begin (GL.QUADS);
    GL.TexCoord2 (0, 0); GL.Vertex3 (0, 0, 0.1);
    GL.TexCoord2 (1, 0); GL.Vertex3 (1, 0, 0.1);
    GL.TexCoord2 (1, 1); GL.Vertex3 (1, 1, 0.1);
    GL.TexCoord2 (0, 1); GL.Vertex3 (0, 1, 0.1);
    GL.End ();

    GL.PopMatrix ();
    }
    @script RequireComponent(Camera)

    I'm only assuming its like 18 of this script and the ending is a +. Knowing other scripting languages, common sense would tell me that ; in there is gonna mess up whatever equation + is part of. Only difference between this script and whats on that page, i renamed the variables accordingly to send it to the proper camera and change the spacebar to c keypress. Gotta keep it simple for me here to cuz i already got this game this far, don't really wanna have to completely rewrite stuff to make 1 simple function work right.

    Ok an update on that I got the script to stop erroring on me, seems those aren't the variables. What am I changing here to have it select the proper camera? One is labeled "Camera-Minimap" the other is "Camera-Chase" in the hierarchy there.
     
    Last edited: Oct 20, 2014
  15. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    Unity has no components named Camera-Chase or Camera-Minimap

    I believe the first two lines should be

    Code (CSharp):
    1. var cam1 : Camera;
    2. var cam2 : Camera;
     
  16. N2KMaster

    N2KMaster

    Joined:
    Jul 15, 2013
    Posts:
    64
    Yeah i figured that part out myself. And figured out where it is to select the camera, however im getting all kinds of weird stuff happening how like blooming and blurring and its not showing me the proper camera, sometimes its simply giving me a gray screen. That script does not seem to wanna work for me. Is it snapped to the camera's itself or to to the like the car itself. It starts now on the wrong camera, switching it to the second camera gives me these weird screens like that, it snaps out of it, goes to the proper camera, switching back to the 1st camera is fine from there, and then switching again gives me them blurred up double imposed screens.

    Update yet again, seemed to have gotten it to work attaching it to the game item however ever time now that it goes to the 2nd camera the car jumps now.....
     

    Attached Files:

    Last edited: Oct 20, 2014