x


Scoping script /Camera zoom preferably C#

does anyone have a script for somthing like using the scope on a sniper do i just need to do somthing like have a 2nd camera futher forward from my gun and when u press fire2/ right click it switchs camera but uses same shooting spawn point if so how would i script that camera move thing

if u could id prefer the script in C# if u cant dw any type will do

thankyou

more ▼

asked Nov 01 '10 at 01:30 PM

Daniel Girgis gravatar image

Daniel Girgis
51 13 13 17

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can implement whatever kind of zoom you want. If you want a dolly-style zoom, then a second camera or simply moving your main camera forward would do and likewise, even displaying a full-screen render texture of the second camera would also be a solution. If you wanted an actual perspective zoom, you would change field of view.

If you were to simply check other questions using the zoom tag which you are already using, you will find several scripts already written to do exactly that. You could also check the questions tagged sniper, but that's a worse tag by far.

Field of view shift would be something like:

//Somewhere
float altFieldOfView = 80.0f;

//Somewhere else
if(Input.GetMouseButtonUp(1)) {
    float temp = camera.fieldOfView;
    camera.fieldOfView = altFieldOfView;
    altFieldOfView = temp;
}

With 2 cameras, you could swap cameras by enabling/disabling them:

//Somewhere in a MonoBehaviour class
public Camera cam1;
public Camera cam2;

//Somewhere else in the class
if(Input.GetButtonUp("Fire2")) {
    cam1.enabled = !cam1.enabled;
    cam2.enabled = !cam2.enabled;
}

As I stated, there are several different solutions, most of which discussed in the questions tagged zoom.

more ▼

answered Nov 01 '10 at 02:22 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

is there a way i can make it so i can switch to the front camera but theres a string so if it hits and object infron it draws back to that and has a set distance limit of the camera thanks

Nov 02 '10 at 06:38 AM Daniel Girgis

No, not really. You'd have to do a raycast from cam1 to cam2 to check for collision and you'd have to do it every frame to prevent something passing between your cameras. Also, unless your cameras are orthographic (in which case there's no point in zooming), this would just look wrong, especially with your camera jumping back like that. Changing field of view is actually the correct implementation of a zoom. If you need it to be a second camera for some reason, why no just have the 2 cameras in the same position/orientation (one the parent of the other), each with different field of view?

Nov 02 '10 at 02:22 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

You don't need a second camera (although you may want one positioned to align with the scope/have a GUITexture drawn over it, etc. That's all up to you, though). All you need to do is have a look at the Camera class, where you can chose to either change the field of view or the camera's transform, depending on how you want the effect to look. If you search the answers site for "sniper," "fov," or "field of view" (or some permutation of the three) you'll get a lot of answers related to what you want, including this one that Jonas answered: link.

more ▼

answered Nov 01 '10 at 02:22 PM

burnumd gravatar image

burnumd
3.3k 22 34 71

thats what i was thinking of doing its just the problem that i could have my camera on the other side of objects ect if its a sniper but thanks

Nov 02 '10 at 05:25 AM Daniel Girgis

Are you talking about having objects occlude the sniper view? If that's the case, you probably want to go with changing the FoV rather than translating your scope's camera.

Nov 02 '10 at 01:37 PM burnumd
(comments are locked)
10|3000 characters needed characters left

i made this out of the script u gave me but is there anyway of making the it actually zoom in like saying feild of view goes up by 2 every frame until it reaches a set number and vice versa sorry i got u doing all my work but im really learning thanks

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    //Somewhere
    float altFieldOfView = 60.0f;
    float ZoomLevel = 10.0f;

        //Somewhere else
    if(Input.GetMouseButtonUp(1)) {
        float temp = camera.fieldOfView;
        camera.fieldOfView = altFieldOfView;
        altFieldOfView = temp;
}
    if(Input.GetMouseButtonDown(1)) {
        float temp = camera.fieldOfView;
        camera.fieldOfView = ZoomLevel;
        ZoomLevel = temp;
        }
    }
}
more ▼

answered Nov 03 '10 at 12:47 AM

Daniel Girgis gravatar image

Daniel Girgis
51 13 13 17

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3336
x3013
x183
x23

asked: Nov 01 '10 at 01:30 PM

Seen: 2605 times

Last Updated: Nov 01 '10 at 01:30 PM