how do you zoom with a sniper?

when zooming with a sniper how do you make the screen black appart from a texture of a scope in the middle?

help is very much appreciated!

http://unity3d.com/support/documentation/Components/class-GuiTexture.html Should help with how to display a full screen texture, as suggested by Andre.

In project view, find "Standard Assets\Blob-Shadow\Shadow" (assuming you included standard assets in your project). Highlight it by clicking on it.

Now click GameObject->Create Other->GUI Texture

If you hit play right now, you'll see a white square with a fuzzy transparent center. This is the wrong size and color, though, so...

Choose Blob Shadow in the Hierarchy view (i.e. the instantialized component in your scene). Rename it to something better (F2 -> "SniperScopeFuzzyOutline")

Now look at it in the Inspector so that you can edit the actual GUITexture component.

In the GUITexture component, select the colour. Turn it to RGB black, but Alpha 255. This will multiply the white texture by zero, which results in a black outline.

Now to change the size - move the X and Y (top left) co-ordinates to (-800, -800). Make the width and height double that (1600,1600) so that the center of the texture falls in the center of your screen (which I assume you'll use to cast your bullets out of - though some FPSs like Halo bucks this trend).

Hit play.

Tweak until you're happy. Change the texture that the GUITexture points to with your own if you want to adjust how quickly the edges get fuzzy - this is just a quick way to get a basic view set up for you.

Repeat the process with your own imported texture for the crosshair (which you might want to keep separate from the fuzzy scope for other reasons).

My suggestion would be to use a texture over the screen giving you the crosshair and blacked out scope view. Also make sure to narrow the field of view on your camera so that the player also gets a distorted view of the world.

For the FOV change, you need to access the camera component's Field Of View value.

Let's set up an input to control it, first:

Go to Edit->Project Settings->Input

This opens the Input Manager

Change the "Size" value to 1 plus whatever it is. This will create an empty input for us to use. It will appear at the bottom with the same name as the last thing in the list. Change this name (the first option when you drop down) to "Zoom".

Click in the "positive button" field, then press the button you want to press in order to press this. I'm going to use "mouse 1" for now (which is the right mouse button... LMB is "mouse 0"). Later I'll add a joystick axis so that left trigger works with it as well. Except then I have to deal with the fact that Microsoft's xbox drivers are intentionally junk, and tie both triggers to one axis. Ahargh. Anyway. That can be fixed quite easily with XInput extensions, so it's no biggy.

Carrying on...

Make the "Type" a "Key or Mouse Button". Ignore the "axis" one, as it won't be used if you've got "Key or Mouse Button" selected. Equally, "Joy Num" doesn't matter for this input. It's the RMB after all.

We're going to do a neat trick here, which involves treating the Zoom button as if it's an axis, rather than just a digital input. This is to fake the "Easing in" and "easing out" of the FOV, so that we don't just snap the gun into ironsight immediately. Unity can fake this really well. Normally, to treat a button like it's digital, you set the Sensitivity (the "ease in" speed) to like, 1000, and the gravity (the "ease out") to the same. A good interactive rate (so that we don't transition for too long that it starts to feel sluggish) is typically less than 250ms. I like to keep it a bit tighter, so I want it to take 200ms to ease in and out. 200ms = 1second / 5. So I'm going to set my Gravity and Sensitivity values to 5 each. In 200ms, I will ease in and out. And if I release half into the ease in, it'll return back to zero from the point I stopped pressing. Responsiveness ftw!

We set up an an input to feed our behaviour from. Now let's gain access to the camera's field of view.

In your project, find a nice place to create a "CameraZoomButton" script component. I'm going to make a c# one because I am used to that (Right Click->Create->C Sharp Script). You might prefer java. Good for you.

Rename "NewBehaviourScript" to "CameraZoomButton"

Double click on it to edit it.

Here's what I've written up with inline comments.

using UnityEngine;
using System.Collections;

//Note, class name must be the same as the file name. In Java, this is sorted naturally.
public class CameraZoomButton : MonoBehaviour
{

    //These values will be exposed to the component so that you can tweak them in real time. Joy!
    public float NormalFOV = 90.0f;
    public float ZoomedFOV = 45.0f;

    //Update is called once per frame
    void Update () {
        Camera CamComponent = GetComponent<Camera>();//We're going to need to grab the camera component associated with this object. See what that entails for the final step, here.

        //Check that we actually found a camera component
        if (CamComponent != null)
        {
            //If we made it here, we found a camera component! Awesome!
            float zoomInput = Input.GetAxis("Zoom");//Here's how we grab the current value of the input we created.

            //Just in case something whacky goes on with the axis, we clamp zoomInput to the range 0..1
            zoomInput = Mathf.Clamp01(zoomInput);

            //Optional: Here you could mess with curving the zoomInput a bit. It currently moves very straight from 0..1. Sometimes a straight linear interpolation can look "robotic".
            //Add "zoomInput = Mathf.Pow(zoomInput, 0.5f);", for a decceleration curve, for example. 

            //Use zoomInput to control a linear interpolation between our target zooms.
            //This "blends" finalZoom between the normal fov, and zoomed fov, using "zoomInput" (which we know is in the range 0..1) as the blending factor - kind of a "slider" between extremes.
            float finalZoom = Mathf.Lerp(NormalFOV, ZoomedFOV, zoomInput);

            //Make the camera use our final zoom
            CamComponent.fov = finalZoom;
        }
        else {
            //Warn ourselfs that we gone done it wrong.
            Debug.LogError("Camera Component not found. Did you add this component to a camera?");
        }

    }

}

Final step: add this component to your camera in your scene.

Select your main camera, then click "Component->Scripts->Camera Zoom Button"

Hit play. Zooming should work!

Next, I'll see how you control the GUI's opacity.

First off, just for the sake of clarity in my project, I'm going to make the SniperFuzzyOutline game object part and parcel of my camera.

Then I'm going to add another little script, very similar to the last one, which changes the opacity of the gui texture involved in pretty much the same way as the FOV zoom. Copy pasta time!

Add another c sharp script called "CameraZoomOpacity". This is going to be very similar to the process of the last one, but we'll put the script on the SniperFuzzyOutline, and access its GUITexture component.

using UnityEngine;
using System.Collections;

public class CameraZoomOpacity : MonoBehaviour {

    public Color unZoomedColor = new Color(0.0f,0.0f,0.0f,0.0f);//Color when unpressed
    public Color ZoomedColor = new Color(0.0f,0.0f,0.0f,1.0f);//Color when pressed

    //Update is called once per frame
    void Update () {

        GUITexture FuzzyZoomTexture = GetComponent<GUITexture>();//This grabs just the first texture in this component.
            if( FuzzyZoomTexture != null ){

            //If we made it here, we found a GUITexture component! Awesome!
            float zoomInput = Input.GetAxis("Zoom");//Here's how we grab the current value of the input we created.

            //Just in case something wacky goes on with the axis, we clamp zoomInput to the range 0..1
            zoomInput = Mathf.Clamp01(zoomInput);
            Color finalColor = Color.Lerp(unZoomedColor, ZoomedColor, zoomInput);//I love lerping colours! We blend between colours just like we blend between

            //Make the camera use our final zoom
            FuzzyZoomTexture.color = finalColor;
        } else {
            //No texture component found. Warn us.
            Debug.LogError("Please hook up the CameraZoomOpacity script to a game object which has a GUITexture component");
        }
    }

}

Add this script to the ZoomGui component!

Some of this stuff could probably be organized slightly nicer, but I am learning this as I answer your question.

This is really freaking cool, and so simple! Thanks for doing this!