枪没有缩小

Gun doesn't zoom out

我的武器缩放脚本有问题。我已经坚持了几个小时了。我访问了很多网站希望能解决我的问题,但都无济于事!

我认为问题与 Unity 无关,而是与我的脚本有关。当我放大(按住右键单击)时代码工作得很好,但当我释放右键单击并且动画播放完毕时不会缩小。它保持放大!一旦动画结束并且我释放右键单击,武器保持放大。

zoomIn() 函数工作正常,但枪在 zoomOut() 函数期间不会缩小。我知道 zoomOut() 函数工作正常,因为相机的 FOV 重置回原来的状态 (60),但动画不会倒带(可能是因为它已停止?)。我试过改变动画的时间,改变它的速度和倒带等等。如果我完全放大并再次放大(我在动画播放完后单击鼠标右键),枪会跳回其原始位置并再次播放缩放动画。

脚本对我来说非常有意义,所以我不知道发生了什么或如何修复它!

下面是我的代码:

 #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;
     }
 }

请帮忙

提前致谢

您正在尝试使用 Animation.Rewind。这只会倒带动画,但不会(AFAIK)反向播放动画

试试这个。

将您的 zoomIn() 和 zoomOut() 方法替换为以下

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;
 }