- Home /
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
Answer by skovacs1 · Nov 01, 2010 at 02:22 PM
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.
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
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 zoo$$anonymous$$g), 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?
Answer by burnumd · Nov 01, 2010 at 02:22 PM
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.
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
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.
Answer by Daniel Girgis · Nov 03, 2010 at 12:47 AM
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; } } }
Your answer
Follow this Question
Related Questions
Camera Zoom Input change. 1 Answer
How to move camera on local z axis 2 Answers
Sniper Zoom Problem 1 Answer
Need to limit zoom in/out of camera script 4 Answers
How to add sniper zoom effect in an FPS made with Unity3d? 2 Answers