Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scope wont zoom in

Discussion in 'Scripting' started by Dragonkkealk, Feb 25, 2013.

  1. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    Hello, I am trying to make a scope for a gun, and have it zoom in a little bit... I have the variable "zoomangle" but it doesn't seem to even acknowledge it. Here is my code for the
    Gun:
    Code (csharp):
    1. var cameraObject : GameObject;
    2. @HideInInspector
    3. var targetXRotation : float;
    4. @HideInInspector
    5. var targetYRotation : float;
    6. @HideInInspector
    7. var targetXRotationV : float;
    8. @HideInInspector
    9. var targetYRotationV : float;
    10.  
    11. var rotateSpeed : float = 0.3;
    12.  
    13. var holdHeight : float = -0.5;
    14. var holdSide : float = 0.5;
    15. var racioHipHold : float = 1;
    16. var hipToAimSpeed : float = 0.1;
    17. @HideInInspector
    18. var racioHipHoldV : float;
    19.  
    20. var aimRacio : float = 0.4;
    21.  
    22. var zoomAngle : float = 30;
    23.  
    24. function Update ()
    25. {
    26.     cameraObject.GetComponent(MouseLookScript).current  TargetCameraAngle = zoomAngle;
    27.    
    28.     if (Input.GetButton("Fire2")){
    29.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    30.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    31.     if (Input.GetButton("Fire2") == false){
    32.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    33.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    34.    
    35.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold, 0));
    36.    
    37.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    38.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    39.    
    40.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    41. }
    and camera
    Code (csharp):
    1. var defaultCameraAngle : float = 60;
    2. @HideInInspector
    3. var currentTargetCameraAngle : float = 60;
    4. @HideInInspector
    5. var racioZoom : float = 1;
    6. @HideInInspector
    7. var racioZoomV : float;
    8. var racioZoomSpeed : float = 0.2;
    9.  
    10. var lookSensitivity : float = 5;
    11. @HideInInspector
    12. var yRotation : float;
    13. @HideInInspector
    14. var xRotation : float;
    15. @HideInInspector
    16. var currentYRotation : float;
    17. @HideInInspector
    18. var currentXRotation : float;
    19. @HideInInspector
    20. var yRotationV : float;
    21. @HideInInspector
    22. var xRotationV : float;
    23. var lookSmoothDamp : float = 0.1;
    24. @HideInInspector
    25. var currentAimRacio : float = 1;
    26.  
    27. function Update ()
    28. {
    29.     if (currentAimRacio == 1)
    30.         racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
    31.     else
    32.         racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
    33.        
    34.     camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    35.  
    36.     yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    37.     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    38.    
    39.     xRotation = Mathf.Clamp(xRotation, -90, 90);
    40.    
    41.     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    42.     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    43.    
    44.     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    45. }
     
  2. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Does this work? (the new gun script)
     
  3. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    "Assets/Gunscript.js(26,55): UCE0001: ';' expected. instert a semicolon at the end."
    I've been getting this error and I'm irritated as hell... THERE IS ALREADY A FRAKKING SEMI-COLON!
     
  4. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    As far as I'm concerned, that error may as well read: "There is something wrong with this line. Fix it."

    Anyways, you left a space at this line between current and TargetCameraAngle.
     
  5. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    OHHHHHHHHHHHHHHHHHHHHHHHHHHHHH! ok that makes sense now...

    Thank you!
     
  6. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    Now I have another problem... It zooms in but it takes a while to zoom back out... Also, I cannot move while aiming.. (that could be because of a variable I have set, I'll check)

    EDIT
    Just checked, Nope, no clue why I can't aim while aiming...
     
  7. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818

    Do you mean it smoothly goes back out? In that case, just lower your "hipToAimSpeed" variable.
     
  8. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    No, I let go of right click and it takes a while before it zooms out... when it does, it does it at a regular rate. But it takes a while for it to happen
     
  9. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    What about (for the camera)

    Code (csharp):
    1.     var defaultCameraAngle : float = 60;
    2.     @HideInInspector
    3.     var currentTargetCameraAngle : float = 60;
    4.     @HideInInspector
    5.     var racioZoom : float = 1;
    6.     @HideInInspector
    7.     var racioZoomV : float;
    8.     var racioZoomSpeed : float = 0.2;
    9.      
    10.     var lookSensitivity : float = 5;
    11.     @HideInInspector
    12.     var yRotation : float;
    13.     @HideInInspector
    14.     var xRotation : float;
    15.     @HideInInspector
    16.     var currentYRotation : float;
    17.     @HideInInspector
    18.     var currentXRotation : float;
    19.     @HideInInspector
    20.     var yRotationV : float;
    21.     @HideInInspector
    22.     var xRotationV : float;
    23.     var lookSmoothDamp : float = 0.1;
    24.     @HideInInspector
    25.     var currentAimRacio : float = 1;
    26.      
    27.     function Update ()
    28.     {
    29.        
    30.         racioZoom = Mathf.SmoothDamp(racioZoom, currentAimRacio , racioZoomV, racioZoomSpeed);
    31.            
    32.         camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    33.      
    34.         yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
    35.         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
    36.        
    37.         xRotation = Mathf.Clamp(xRotation, -90, 90);
    38.        
    39.         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    40.         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    41.        
    42.         transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    43.     }
     
  10. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    Nope, still can't aim while zoomed...
     
  11. SimtropBuggi

    SimtropBuggi

    Joined:
    Feb 15, 2013
    Posts:
    98
    what is 'racio'?

    Is that supposed to be 'ratio'? as in a %?
     
  12. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    It's a mistake in Eteeski's tutorials that everyone looked funny at, said "meh, maybe it was spelled like that for a reason" and moved on.

    What do you mean by can't aim? Can't turn? Can't zoom out? No ADS?
     
  13. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    As in can't turn... Sorry for not specifying xP
     
  14. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Code (csharp):
    1.         var defaultCameraAngle : float = 60;
    2.         @HideInInspector
    3.         var currentTargetCameraAngle : float = 60;
    4.         @HideInInspector
    5.         var racioZoom : float = 1;
    6.         @HideInInspector
    7.         var racioZoomV : float;
    8.         var racioZoomSpeed : float = 0.2;
    9.          
    10.         var lookSensitivity : float = 5;
    11.         @HideInInspector
    12.         var yRotation : float;
    13.         @HideInInspector
    14.         var xRotation : float;
    15.         @HideInInspector
    16.         var currentYRotation : float;
    17.         @HideInInspector
    18.         var currentXRotation : float;
    19.         @HideInInspector
    20.         var yRotationV : float;
    21.         @HideInInspector
    22.         var xRotationV : float;
    23.         var lookSmoothDamp : float = 0.1;
    24.         @HideInInspector
    25.         var currentAimRacio : float = 1;
    26.          
    27.         function Update ()
    28.         {
    29.            
    30.             racioZoom = Mathf.SmoothDamp(racioZoom, currentAimRacio , racioZoomV, racioZoomSpeed);
    31.                
    32.             camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);
    33.          
    34.             yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
    35.             xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
    36.            
    37.             xRotation = Mathf.Clamp(xRotation, -90, 90);
    38.            
    39.             currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
    40.             currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
    41.            
    42.             transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    43.         }
    Just to let you know, I have to do a lot of guessing, as I don't have the game in front of me. Anyways, try this camera script.
     
  15. Dragonkkealk

    Dragonkkealk

    Joined:
    Feb 23, 2013
    Posts:
    17
    Works great! THANKS!
     
  16. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Glad to hear that! :)