Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么协程方法只工作一次Unity3D_C#_Unity3d - Fatal编程技术网

C# 为什么协程方法只工作一次Unity3D

C# 为什么协程方法只工作一次Unity3D,c#,unity3d,C#,Unity3d,我有一个我想通过滑动移动的对象,例如,当滑动向上时,对象应该从A点平滑地向前移动到B点,向右滑动对象平滑地向右移动等等 为此,我尝试了Lerp,移动到和平滑阻尼,但每次对象从点A消失并立即出现在点B上 所以我使用了协程来给运动一个时间,正如你在下面的代码中看到的,共有4种协程方法,每一种都代表一个方向。我遇到的问题是,在玩的时候,第一个动作正常,但是在第二个动作中,物体没有到达目的地,第三个动作和物体也有一些奇怪的动作 你能告诉我我的密码出了什么问题吗 以下是运动的协同规划方法: public

我有一个我想通过滑动移动的对象,例如,当滑动向上时,对象应该从A点平滑地向前移动到B点,向右滑动对象平滑地向右移动等等

为此,我尝试了Lerp移动到平滑阻尼,但每次对象从点A消失并立即出现在点B上

所以我使用了协程来给运动一个时间,正如你在下面的代码中看到的,共有4种协程方法,每一种都代表一个方向。我遇到的问题是,在玩的时候,第一个动作正常,但是在第二个动作中,物体没有到达目的地,第三个动作和物体也有一些奇怪的动作

你能告诉我我的密码出了什么问题吗

以下是运动的协同规划方法:

public IEnumerator MoveForward()
{
    Vector3 DestinationF = new Vector3(transform.position.x, transform.position.y, transform.position.z + DistanceF); 
    while (Vector3.Distance(transform.localPosition, DestinationF) > 0)
    {
        float totalMovementTimeF = 0.3f;
        float currentMovementTimeF = 0f;
        currentMovementTimeF += Time.deltaTime;
        transform.localPosition = Vector3.Lerp(transform.position, DestinationF, currentMovementTimeF / totalMovementTimeF);
        yield return null;
    }
}
public IEnumerator MoveBackward()
{
    Vector3 DestinationB = new Vector3(transform.position.x, transform.position.y, transform.position.z - DistanceB);
    while (Vector3.Distance(transform.localPosition, DestinationB) > 0)
    {
        float totalMovementTimeB = 0.3f;
        float currentMovementTimeB = 0f;
        currentMovementTimeB += Time.deltaTime;
        transform.localPosition = Vector3.Lerp(transform.position, DestinationB, currentMovementTimeB / totalMovementTimeB);
        yield return null;
    }
}
if (Input.GetMouseButtonDown(0))
    {
        //save began touch 2d point
        firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
    }
    if (Input.GetMouseButtonUp(0))
    {
        //save ended touch 2d point
        secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);

        //create vector from the two points
        currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

        //normalize the 2d vector
        currentSwipe.Normalize();

        // swipe up
        if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
        {
            StartCoroutine(MoveForward());
        }

        // swipe down
        if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
        {
            StartCoroutine(MoveBackward());
        }

        //swipe left
        if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
        {
            StartCoroutine(MoveLeft());
        }

        //swipe right
        if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
        {
            StartCoroutine(MoveRight());
        }

    }
还有两个协同路由方法MoveRight()和MoveLeft()

这是刷卡方向的代码:

public IEnumerator MoveForward()
{
    Vector3 DestinationF = new Vector3(transform.position.x, transform.position.y, transform.position.z + DistanceF); 
    while (Vector3.Distance(transform.localPosition, DestinationF) > 0)
    {
        float totalMovementTimeF = 0.3f;
        float currentMovementTimeF = 0f;
        currentMovementTimeF += Time.deltaTime;
        transform.localPosition = Vector3.Lerp(transform.position, DestinationF, currentMovementTimeF / totalMovementTimeF);
        yield return null;
    }
}
public IEnumerator MoveBackward()
{
    Vector3 DestinationB = new Vector3(transform.position.x, transform.position.y, transform.position.z - DistanceB);
    while (Vector3.Distance(transform.localPosition, DestinationB) > 0)
    {
        float totalMovementTimeB = 0.3f;
        float currentMovementTimeB = 0f;
        currentMovementTimeB += Time.deltaTime;
        transform.localPosition = Vector3.Lerp(transform.position, DestinationB, currentMovementTimeB / totalMovementTimeB);
        yield return null;
    }
}
if (Input.GetMouseButtonDown(0))
    {
        //save began touch 2d point
        firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
    }
    if (Input.GetMouseButtonUp(0))
    {
        //save ended touch 2d point
        secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);

        //create vector from the two points
        currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

        //normalize the 2d vector
        currentSwipe.Normalize();

        // swipe up
        if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
        {
            StartCoroutine(MoveForward());
        }

        // swipe down
        if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
        {
            StartCoroutine(MoveBackward());
        }

        //swipe left
        if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
        {
            StartCoroutine(MoveLeft());
        }

        //swipe right
        if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
        {
            StartCoroutine(MoveRight());
        }

    }
if(Input.GetMouseButtonDown(0))
{
//保存开始接触二维点
firstPressPos=新矢量3(Input.mousePosition.x,Input.mousePosition.y);
}
if(Input.GetMouseButtonUp(0))
{
//保存结束的触摸2d点
secondPressPos=新矢量3(Input.mousePosition.x,Input.mousePosition.y);
//从两点创建向量
当前滑动=新矢量3(第二按位置x-第一按位置x,第二按位置y-第一按位置y);
//规范化二维向量
currentSwipe.Normalize();
//刷卡
如果(currentSwipe.y>0&¤tSwipe.x>-0.5f&¤tSwipe.x<0.5f)
{
start例程(MoveForward());
}
//向下滑动
如果(currentSwipe.y<0&¤tSwipe.x>-0.5f&¤tSwipe.x<0.5f)
{
start例程(MoveBackward());
}
//左击
如果(currentSwipe.x<0&¤tSwipe.y>-0.5f&¤tSwipe.y<0.5f)
{
start例程(MoveLeft());
}
//向右滑动
如果(currentSwipe.x>0&¤tSwipe.y>-0.5f&¤tSwipe.y<0.5f)
{
start例程(MoveRight());
}
}

您的第一个协同程序之所以有效,是因为: Vector3 DestinationF=新的Vector3(transform.position.x,transform.position.y,transform.position.z+DistanceF)

将产生一个正位置,因此距离将大于0:

while (Vector3.Distance(transform.localPosition, DestinationF) > 0)
另一方面,从z值中减去距离B时:

Vector3 DestinationB = new Vector3(transform.position.x, transform.position.y, transform.position.z - DistanceB);
可能导致负值,因此:

while (Vector3.Distance(transform.localPosition, DestinationB) > 0)

将从<0开始,因此永远不满足该条件。检查你的情况。您想要绝对值,还是不等于0?

问题是您从未达到目标

你对因素的反应

currentMovementTimeF/totalMovementTimeF
没有多大意义,因为每帧都将其重置为

var currentMovementTimeF=Time.deltaTime;
在大多数情况下,它将是
<0.3f
(这意味着每秒只有大约3帧),因此它将始终是

currentMovementTimeF
因此

currentMovementTimeF/totalMovementTimeF<1
因此,您总是在当前位置和目标之间开始一个新的插值。所以距离越来越小,但实际上从未到达最终位置(尽管看起来如此)

此外,您在那里混合了
位置
本地位置
,因此如果
游戏对象
不在根级别,情况会变得更糟


相反,您可能希望以一定的
速度使用。(基于职位)

//在Inspector中调整这些
公众浮标速度;
公共交通距离;
公共IEnumerator移动(矢量3方向)
{
var destinaton=变换位置+方向*移动距离;
而(矢量3.距离(变换.位置,目标)>0)
{
transform.position=Vector3.moveToward(transform.position、MoveDistance、Time.deltaTime*速度);
收益返回空;
}
}
movetoward
确保没有超调

或者使用Lerp(基于时间的),比如

//在Inspector中调整这些
公共浮动总移动时间=0.3f;
公共交通距离;
公共IEnumerator移动(矢量3方向)
{
var originalPosition=transform.position;
var destination=变换位置+方向*移动距离;
//在这里,你可以使它更准确
//始终以相同的速度移动
//无论对象离目标有多远
//var moveDuration=totalMovementTime*Vector3.距离(transform.position,destinaton);
//而不是将totalMovementTime替换为moveDuration
var currentDuration=0.0f;
while(currentDuration

另一个问题是,您目前仍然可以启动两个并发的协同程序,这会导致奇怪的行为。你应该在每次你开始一个新的合作项目时打断它,比如

if(currentSwipe.y>0&¤tSwipe.x>-0.5f&¤tSwipe.x<0.5f)
{
//停止所有当前例程
stopAllCoroutines();
start例程(MoveForward());
}
或者添加一个标志,使其仅运行一个例程,同时忽略输入:

private bool isswip;
公共IEnumerator向前移动()
{
如果(正在擦除)
{
LogWarning(“已刷卡->忽略”,此选项);