Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CodyMoores · Mar 03, 2014 at 05:00 AM · zoomfovfieldofviewscopesensitivity

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoahConstable · Mar 03, 2014 at 05:56 AM 0
Share

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.

avatar image CodyMoores · Mar 03, 2014 at 11:54 AM 0
Share

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

avatar image CodyMoores · Mar 03, 2014 at 11:16 PM 0
Share
 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;
          }
       } 
 }
 
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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


codymoores scope package.zip (6.6 kB)
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoahConstable · Mar 04, 2014 at 05:08 AM 0
Share

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.

avatar image CodyMoores · Mar 04, 2014 at 10:51 AM 0
Share

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 :)

avatar image CodyMoores · Mar 04, 2014 at 10:54 AM 0
Share

Now it doens't zoom in it justs changes to the camera with no zoom :$

avatar image NoahConstable · Mar 04, 2014 at 12:52 PM 0
Share

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.

avatar image CodyMoores · Mar 04, 2014 at 01:12 PM 0
Share

Yup did it all. I'll have to play with it

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lining up Fov's 0 Answers

Change field of view over time 1 Answer

Sniper Zooming 2 Answers

Negative results when converting HFOV to VFOV? 1 Answer

Could anyone help me with this weapon script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges