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
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.
Comments