【问题标题】:Unity3D: How to stop the flickering that occurs when the character stops moving?Unity3D:如何停止角色停止移动时发生的闪烁?
【发布时间】:2016-08-09 01:58:53
【问题描述】:

我在youtube 上关注了本教程。我花了一段时间,但我几乎已经从箭头移动转换为鼠标触摸。但是现在我遇到了这个问题,当角色停止移动时会发生闪烁。我查看了他发给我的链接,但它不起作用。请观看此视频以向您展示我在说什么about。有谁知道如何停止角色停止移动时发生的闪烁?这是我的代码:

private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;


void Start () {
    target = transform.position;
    anim = GetComponent<Animator> ();
}

void Update () {
    touched = true;
    if (Input.GetMouseButtonDown (0)) {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 10; // distance from the camera
        target = Camera.main.ScreenToWorldPoint(mousePosition);
        target.z = transform.position.z;
    }



        var movementDirection = (target - transform.position).normalized;

        if (movementDirection.x != 0 || movementDirection.y != 0) {
            anim.SetBool("walking" , true);
            anim.SetFloat("SpeedX" , movementDirection.x);
            anim.SetFloat("SpeedY" , movementDirection.y);

            Vector2 movement = new Vector2(
                speed * movementDirection.x ,
                speed * movementDirection.y);
            movement *= Time.deltaTime;
            transform.Translate(movement);

            if (movementDirection.x < 0) {
                anim.SetFloat("LastMoveX" , -1f);
            }
            else if (movementDirection.x > 0) {
                anim.SetFloat("LastMoveX" , 1f);
            }
            else {
                anim.SetFloat("LastMoveX" , 0f);
            }
            if (movementDirection.y > 0) {
                anim.SetFloat("LastMoveY" , 1f);
            }
            else if (movementDirection.y < 0) {
                anim.SetFloat("LastMoveY" , -1f);
            }
            else {
                anim.SetFloat("LastMoveY" , 0f);
            }
        } else {
        touched = false;
        anim.SetBool("walking" , false);
    }
}

【问题讨论】:

    标签: c# unity3d youtube


    【解决方案1】:

    我会说这是因为你的角色从来没有真正停在目标位置。

    根据我从您的代码中得到的信息,每帧更新您的角色都会通过评估“movementDirection”在 x 和 y 轴上是否有任何幅度来验证变量“target”是否改变了它的位置。

    当它使用变量“运动”来计算朝向预定目的地的位置增量时,对吗?但是,当您的角色对象非常接近目的地时,在行后“移动”处计算的增量

    movement *= Time.deltaTime;
    

    对于剩余的移动距离来说太大了,所以你的角色超出了目标位置。另一种说法是:

    假设你的角色的速度是 3(为了这个论点)。那么你有:

    (C是你的角色,T是目标)

    | C | | | | | | | | T | | | | | | |

    那么,

    | | | | C | | | | T | | | | | | |

    那么,

    | | | | | | | C | T | | | | | | |

    然后,

    | | | | | | | | T | | C | | | | |

    如您所见,它的超速使其通过目标位置。你的脚本接下来会做什么?它将反转运动方向,因此您将拥有:

    | | | | | | C | T | | | | | | |再次

    然后,

    | | | | | | | T | | C | | | | |

    然后,

    | | | | | | C | T | | | | | | |

    所以你的角色永远被锁定在这个循环中,总是在目标附近的这两个位置之间变化,这会产生你看到的闪烁效果。

    建议:

    1. 改变计算角色运动的方式,例如不使用翻译,添加刚体和使用物理(虽然我知道你不想选择这个)

    2. 编写一个 if 语句来检查字符是否非常接近您的目标。如果是,只需将其移动到该位置,例如

      transform.position = 目标;

    3. 只需降低角色的速度即可。这是最不推荐的选项,因为它现在可能会解决问题,但以后可能会再次发生。

    EDIT1:一开始我的回答就更清楚了。

    EDIT2:纠正了我类比的错误

    【讨论】:

    • 嗨 :) 我的回答对你有帮助吗?你能解决这个问题吗?
    猜你喜欢
    • 2014-01-05
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多