FPS sniper zoom effect without black texture?

Hey guys I’ve been looking through the answers and really couldn’t find the answer to this one.

  1. should I bother animating an aiming animation (bring gun to centre of the screen) or should I just hard code it? (if it’s better to code could someone point me in the right direction to start?)

  2. How would I go about when the player right clicks they get a zoom effect (sniper zoom). Like in CoD but without the black texture around the screen so that they can see what’s going on in the battlefield on the outskirts of the scope?

Sorry if it seems like I need spoon feeding but I would really LOVE some help with this! thanks guys!

Animation can produce more natural and realistic results, but the hard code way may be good enough since aiming is a very simple movement.

A possible hard code way is to place an empty object at the aim position/orientation and child it to the camera. When aiming, Lerp the position and Slerp the rotation to the aiming object, and do the inverse path when returning to the standard position. To zoom in, just change the camera’s Field Of View: narrow angles zoom in, while wider angles zoom out - like this (weapon script):

var aimObj: Transform; // drag the aiming position game object here
var duration: float = 0.6; // animation duration in seconds
var zoomFov: float = 25; // set the zoomed field of view

private var regPos: Vector3;
private var regRot: Quaternion;
private var regFov: float = 60; // standard field of view

function Start(){ // save regular position, rotation and fov:
  regPos = transform.localPosition;
  regRot = transform.localRotation;
  regFov = Camera.main.fieldOfView;
}

private var aimInUse = false;

function Aiming(onOff: boolean){
  if (aimInUse) return; // avoid multiple Aiming calls
  aimInUse = true; // signal that Aiming is running
  var t: float = 0;
  while (t < 1){
    t += Time.deltaTime / duration;
    var r = onOff? t : 1-t; // r goes 0->1 or 1->0, depending on onOff 
    transform.localPosition = Vector3.Lerp(regPos, aimObj.localPosition, r);
    transform.localRotation = Quaternion.Slerp(regRot, aimObj.localRotation, r);
    yield; // resume next frame
  }
  if (onOff){ // change the field of view after animation
    Camera.main.fieldOfView = zoomFov;
  } else {
    Camera.main.fieldOfView = regFov;
  }
  aimInUse = false;
}

Call Aiming(true) to aim and zoom, and Aiming(false) to return to standard position.

Ad.2.
Add camera. I don’t how you’ve got weapons but I can guess that riffle is different prefab.
So add script and in it add function scope to it. In that function you can use the Unity function called GetMouseButtonDown(0). 0 is for the left button, 1 is for the right button and 2 is for the middle button. There’s a corresponding GetMouseButtonUp(0), as well as GetMouseButton(0), which returns true every frame as long as the mouse button is down.

that function should change camera from your main to scope
(or if you want no black texture) you should put that second, round scope camera on your main (cause it will be looking stupid zooming outside the scope).

So you should have two cameras and as long as right button is pressed you should be able to scope stuff - but that’s only my idea :wink:

When you’ll have that you can also add rocket launcher with camera on bullets - that could be fun especially in not so realistic shooter.

Ad.1. I’d go with animation - it’ll definitely looks better. But as I wrote in my comment one can say: code and another will write you: animate.

2:
If you have a “scope” on the model you could use this one too if you want to. You just had to bring the gun in front of the camera and the lerp the Field of view of the camera down to what zoom you want. It maybe looks a bit jerky when you do it because you need a scope on the model which looks good and is big enough. You could also decrease the mouse intensity when zoomed in.

Just to give you an alternative without another camera but the answers above this one work too.