【问题标题】:Gun doesn't zoom out枪没有缩小
【发布时间】:2015-02-28 03:38:46
【问题描述】:

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

我认为问题与 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;
     }
 }

请帮忙

提前致谢

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您正在尝试使用 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;
     }
    

    【讨论】:

    • 它没有用。不过,感谢您的帮助。我通过再次播放动画然后倒带来修复它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2021-12-04
    相关资源
    最近更新 更多