Gun doesn't zoom out

Aaron Garton

I have a problem with my weapon's zoom script. I have been stuck on this for hours now. I have visited many sites in hope of solving my problem, but to no avail!

I don't think the problem has anything to do with Unity, but with my script instead. The code works perfectly fine when I zoom in (holding right click), but doesn't zoom out when I release right click and the animation has finished playing. It stays zoomed in! Once the animation has ended and I release right click, the weapon stays zoomed in.

The zoomIn() function works fine, but the gun doesn't zoom out during the zoomOut() function. I know the zoomOut() function works fine, because the camera's FOV resets back to what it was(60), but the animation doesn't rewind (maybe because it's stopped?). I have tried changing the animation's time, changing its speed and rewinding and many other things. If I am fully zoomed in and I zoom in again (I right click once the animation has finished playing), the gun jumps back to its original position and plays the zoom animation again.

The script makes perfect sense to me, so I don't know what is going on or how to fix it!

Below is my code:

 #pragma strict

 var arms : GameObject;
 var zoomed : boolean = false;

 function Update () {
     if (Input.GetMouseButton(1) && zoomed == false) {
         zoomIn();
     }
     if (!Input.GetMouseButton(1)) {
         zoomOut();
     }
 }

 function zoomIn() {
     if (Input.GetMouseButton(1)) {
         animation.Play("zoom");
         camera.main.fieldOfView = 50;
         arms.active = false;
         yield WaitForSeconds(0.3);
         zoomed = true;
     }
 }

 function zoomOut() {
     zoomed = false;
     if (zoomed == false) {
         animation.Rewind("zoom");
         camera.main.fieldOfView = 60;
         arms.active = true;
     }
 }

Please help

Thanks in advance

Venkat at Axiom Studios

You're trying to use Animation.Rewind. This only rewinds the animation, but does not (AFAIK) play the animation in reverse

Try this out instead.

Replace your zoomIn() and zoomOut() methods with the below

function zoomIn() {
    //A speed of 1 means that the animation will play at 1x in the positive timeline
    animation["zoom"].speed = 1;
    //Set the time to the FIRST key frame.
    animation["zoom"].time = 0;
    animation.Play("zoom");
    camera.main.fieldOfView = 50;
    arms.active = false;
    yield WaitForSeconds(0.3);
    zoomed = true;
}

function zoomOut() {
    zoomed = false;
    //A speed of -1 means that the animation will play the animation at 1x speed in reverse
    animation["zoom"].speed = -1;
    //Set the time to the LAST key frame. Replace the number "10" with the time of your last keyframe
    animation["zoom"].time = 10;
    animation.Play("zoom");
    camera.main.fieldOfView = 60;
    arms.active = true;
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Zoom In - Out doesn´t work correctly

From Dev

pinch zoom doesn't zoom out on move to next fragment page

From Dev

Gun doesn't stay relative to camera in a desirable way

From Dev

Adding new markers on map using cluster manager doesn't reflect the changes until I zoom in and zoom out the map

From Dev

Bullets not getting shot out of the gun

From Dev

Bullets not getting shot out of the gun

From Java

Can't zoom out in visual studio code

From Dev

can't able zoom in out in mobile devices

From Dev

Zoom out and in

From Dev

Office 2013 doesn't zoom in real time?

From Dev

Div/Picture doesn't resize properly on zoom

From Dev

FFmpeg: Zoom with xfade filter doesn't work

From Dev

Amstock chart doesn't zoom on init event

From Dev

Slide out nav doesn't slide out

From Dev

Slide out nav doesn't slide out

From Dev

Why it doesn't fade out?

From Dev

ReentrantLock doesn't work out

From Dev

Don't resize annotation on MKMapView zoom-in/out

From Dev

Zoom In and Zoom out using selenium

From Dev

Setting zoom level doesn't work on Google Map

From Dev

MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

From Dev

Semantic Zoom doesn't work in Windows 8 app

From Dev

Android Google maps Api doesn't uncluster while zoom in

From Dev

google map doesn't appear if browser zoom 110%

From Dev

Matplotlib interactive navigation zoom-to-rectangle button doesn't work

From Dev

MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

From Dev

Image doesn't stay inside border/canvas on drag/zoom

From Dev

Setting zoom level doesn't work on Google Map

From Dev

Okular doesn't show correct zoom factors (too small)

Related Related

  1. 1

    Zoom In - Out doesn´t work correctly

  2. 2

    pinch zoom doesn't zoom out on move to next fragment page

  3. 3

    Gun doesn't stay relative to camera in a desirable way

  4. 4

    Adding new markers on map using cluster manager doesn't reflect the changes until I zoom in and zoom out the map

  5. 5

    Bullets not getting shot out of the gun

  6. 6

    Bullets not getting shot out of the gun

  7. 7

    Can't zoom out in visual studio code

  8. 8

    can't able zoom in out in mobile devices

  9. 9

    Zoom out and in

  10. 10

    Office 2013 doesn't zoom in real time?

  11. 11

    Div/Picture doesn't resize properly on zoom

  12. 12

    FFmpeg: Zoom with xfade filter doesn't work

  13. 13

    Amstock chart doesn't zoom on init event

  14. 14

    Slide out nav doesn't slide out

  15. 15

    Slide out nav doesn't slide out

  16. 16

    Why it doesn't fade out?

  17. 17

    ReentrantLock doesn't work out

  18. 18

    Don't resize annotation on MKMapView zoom-in/out

  19. 19

    Zoom In and Zoom out using selenium

  20. 20

    Setting zoom level doesn't work on Google Map

  21. 21

    MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

  22. 22

    Semantic Zoom doesn't work in Windows 8 app

  23. 23

    Android Google maps Api doesn't uncluster while zoom in

  24. 24

    google map doesn't appear if browser zoom 110%

  25. 25

    Matplotlib interactive navigation zoom-to-rectangle button doesn't work

  26. 26

    MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

  27. 27

    Image doesn't stay inside border/canvas on drag/zoom

  28. 28

    Setting zoom level doesn't work on Google Map

  29. 29

    Okular doesn't show correct zoom factors (too small)

HotTag

Archive