- Home /
Sensitivity Help? || Scope Zoom
Hey guys! Cody here. It seems I am having a few issues. I created a scope on a sniper to get the basics down and I am using the FOV cam. I was wondering if there was a way to modify the zoom so while im zoomed in the sensitivity goes down to about 2 or 3. Heres my script. Feel free to modify or make any changes. Thanks to anyone who helps!
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added.
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class mouseLookAndHide : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
public bool showingMouse;
void Update ()
{
//if you press "r" and the mouse is not shown then the mouse is shown and movable then execute code
if(Input.GetKeyDown(KeyCode.M) && showingMouse == false){
showingMouse = true;
Screen.showCursor =true;
Screen.lockCursor = false;
}
//if you press "r" and the mouse is shown then make it not shown
else if(Input.GetKeyDown(KeyCode.M) && showingMouse == true){
showingMouse = false;
Screen.showCursor =false;
Screen.lockCursor = true;
}
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
showingMouse = false;
Screen.showCursor = false;
Screen.lockCursor = true;
// Locks in center of screen, Hides cursor
//Screen.lockCursor = true;
//Screen.showCursor = false;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
Also I know the code is the original I just implemented some of my own things. Thanks!
Hey Cody! I need to know, do you have a script made for your sniper object already, or are you asking for someone to script the scope functionality for you? If you don't have a script, then you're going to need to either make one, or go over to the Unity Forums and ask for scripting help. If you have a script for the sniper scope already, please post it and I'll help you from there, it sounds like a simple fix.
Ohhh Yeah. $$anonymous$$y bad. I have the script but I'm in school as of now, I'll post it when I'm home. Sorry
Scope:
var Aim : boolean = false;
var Cam : GameObject;
function Update () {
if(Input.Get$$anonymous$$ouseButtonDown(1)){
Aim = true;
if(Aim == true) {
Cam.active = true;
}
}
if(Input.Get$$anonymous$$ouseButtonUp(1)) {
Aim = false;
if(Aim == false) {
Cam.active = false;
}
}
}
Answer by NoahConstable · Mar 04, 2014 at 04:56 AM
Hey Cody! I'm back, and I have the script. In this script you zoom in with the right-mouse button, and when it zooms in the sensitivity changes from 15f, to 5f, and back when you let go of right-click.
Edit: I've added a link to the assets package at the bottom of my answer.
Here's the code in C#:
using UnityEngine;
using System.Collections;
public class Scope : MonoBehaviour {
public int zoom;
public int normal;
public float smoothDamp;
public bool isZoomed;
private float sensitivityX;
private float sensitivityY;
void Update () {
if(Input.GetMouseButton(1)){
isZoomed = true;
if(isZoomed == true) {
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, smoothDamp * Time.deltaTime);
sensitivityY = gameObject.GetComponent<MouseLookAndHide>().sensitivityX = 5f;
sensitivityY = gameObject.GetComponent<MouseLookAndHide>().sensitivityY = 5f;
}
}
if(!Input.GetMouseButton(1)) {
isZoomed = false;
if(isZoomed == false) {
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal, smoothDamp * Time.deltaTime);
sensitivityY = gameObject.GetComponent<MouseLookAndHide>().sensitivityX = 15f;
sensitivityY = gameObject.GetComponent<MouseLookAndHide>().sensitivityY = 15f;
}
}
}
}
Or, you can just download the Unity scripts and scene from my project folder, here: CodyMoores Scope Package
Now, I know this is in C#. In my defense, C# is the language I use the most, and with which I am most comfortable. Also, your "$$anonymous$$ouseLookAndHide" script was in C#. If you need it changed to JavaScript, just ask and it shall be done.
Now, I know this is a very rough script. It is good enough for functionality, but not that great for simplicity. If you need me to simplify and organize it I will. If you can work with this, great!
Let me explain what I did, just in case you need to know:
public int zoom; //FOV while zoo$$anonymous$$g
public int normal; //FOV when not zoo$$anonymous$$g
public float smoothDamp; //Amount (as speed) by which the scope zooms in
public bool isZoomed; //Boolean to check if it's zoomed in
private float sensitivityX; //$$anonymous$$ouse sensitivity of X
private float sensitivityY; //$$anonymous$$ouse sensitivity of Y
Now, for the Void Update function:
void Update () {
//If we right-click and hold it, zoom in
//$$anonymous$$ake isZoomed true
if(Input.Get$$anonymous$$ouseButton(1)){
isZoomed = true;
//If isZoomed is true, zoom in to the zoom amount from our original position, by the speed of smoothDamp (in seconds)
if(isZoomed == true) {
camera.fieldOfView = $$anonymous$$athf.Lerp(camera.fieldOfView, zoom, smoothDamp * Time.deltaTime);
sensitivityY = gameObject.GetComponent<$$anonymous$$ouseLookAndHide>().sensitivityX = 5f;
sensitivityY = gameObject.GetComponent<$$anonymous$$ouseLookAndHide>().sensitivityY = 5f;
}
}
//If we let go of the right-click, zoom out
//$$anonymous$$ake IsZoomed false
if(!Input.Get$$anonymous$$ouseButton(1)) {
isZoomed = false;
//If isZoomed is false, zoom to our original position, from zoomed position, by the speed of smoothDamp (in seconds)
if(isZoomed == false) {
camera.fieldOfView = $$anonymous$$athf.Lerp(camera.fieldOfView, normal, smoothDamp * Time.deltaTime);
sensitivityY = gameObject.GetComponent<$$anonymous$$ouseLookAndHide>().sensitivityX = 15f;
sensitivityY = gameObject.GetComponent<$$anonymous$$ouseLookAndHide>().sensitivityY = 15f;
}
}
}
Hope it's simple enough to understand. If not just make sure you comment and tell me what doesn't work, or what you don't understand. If it works, please mark it as solved, so that others can see it, and know it works, ask their own questions and grow the Unity community.
Cheers! Noah.
It only zooms in when I click right mouse for the first time then stops. And its still really sensitive when I move it. Thank you for the help by the way :)
Now it doens't zoom in it justs changes to the camera with no zoom :$
Strange... I just tested it again and it still works.
$$anonymous$$ake sure you:
Copy my script (From the "Answer" section, not the "Comment" section), and name it "Scope" ($$anonymous$$ake sure it is a C# script).
Add the script (Scope.cs) to your camera object, along with the "$$anonymous$$ouseLookAndHide" script.
$$anonymous$$anually set the variables "Zoom", "Normal" and "Smooth Damp".
Click Play and make sure you are right-clicking when you zoom.
If you need extra help, such as a video, I'll be glad to offer it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Lining up Fov's 0 Answers
Change field of view over time 1 Answer
Sniper Zooming 2 Answers