Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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# 统一在循环中这两个条件都满足,导致对象从左向右移动到;“振动”;_C#_Unity3d - Fatal编程技术网

C# 统一在循环中这两个条件都满足,导致对象从左向右移动到;“振动”;

C# 统一在循环中这两个条件都满足,导致对象从左向右移动到;“振动”;,c#,unity3d,C#,Unity3d,代码在这里,输出在终端中 我不明白为什么它会“振动”,当物体向一侧移动太远时,它会在一个很小的图形之间移动。我注意到有时物体在移动时会振动,这是一个错误 只要将对象移动到更高的值,即可轻松重新创建 在这种情况下 _leftLimit = -8; _rightLimit = 8; 以下是导致问题的协同程序。当我把它移到最左边时,用一根圆木。我很高兴看到你对这件事的看法。谢谢 public IEnumerator Move(float move) { _canMove = true;

代码在这里,输出在终端中

我不明白为什么它会“振动”,当物体向一侧移动太远时,它会在一个很小的图形之间移动。我注意到有时物体在移动时会振动,这是一个错误

只要将对象移动到更高的值,即可轻松重新创建

在这种情况下

 _leftLimit = -8;
 _rightLimit = 8;
以下是导致问题的协同程序。当我把它移到最左边时,用一根圆木。我很高兴看到你对这件事的看法。谢谢

 public IEnumerator Move(float move) {
    _canMove = true;

    float speed = _speed;

    //if this block move left first move left
   if(_blockInitDir == 0) {
       //make speed positive
       //its a positive speed by default

   } else {
       //make speed negative
       speed = -speed;
   }

    while (_canMove) {

        //moved very far left
        if (this.transform.localPosition.x < _rightLimit
            && this.transform.localPosition.x < _leftLimit) {
            speed = speed * -1;
            Debug.Log(speed + " = speed. X = " + this.transform.localPosition.x);
        } 

        //moved very far right
        if (this.transform.localPosition.x > _leftLimit
            && this.transform.localPosition.x > _rightLimit) {
            speed = speed * -1;
            Debug.Log(speed + " = speed. X = " + this.transform.localPosition.x);

        }

        transform.Translate(speed * Time.deltaTime, 0, 0);

        yield return null;
    }


}
公共IEnumerator移动(浮动移动){
_canMove=true;
浮动速度=_速度;
//如果此块向左移动,请先向左移动
如果(_blockInitDir==0){
//使速度为正
//默认为正速度
}否则{
//使速度为负
速度=-速度;
}
而(_canMove){
//向左走了很远
if(this.transform.localPosition.x<\u righlimit
&&this.transform.localPosition.x<\u leftLimit){
速度=速度*-1;
Debug.Log(speed+”=speed.X=“+this.transform.localPosition.X);
} 
//非常向右移动
如果(this.transform.localPosition.x>\u leftLimit
&&this.transform.localPosition.x>\u rightLimit){
速度=速度*-1;
Debug.Log(speed+”=speed.X=“+this.transform.localPosition.X);
}
transform.Translate(速度*Time.deltaTime,0,0);
收益返回空;
}
}
当对象移动到最左侧时,此图像位于控制台中


它的振动是因为只要物体变换超过一个极限,它就会不断地执行速度*-1,从而改变它移动的方向。所以你需要做的不是在它超过极限时反转方向,你必须确保它保持在那个方向。例如,将其更改为

    if (this.transform.localPosition.x < _rightLimit
        && this.transform.localPosition.x < _leftLimit && speed < 0) {
        speed = speed * -1;
        Debug.Log(speed + " = speed. X = " + this.transform.localPosition.x);
    } 

    //moved very far right
    if (this.transform.localPosition.x > _leftLimit
        && this.transform.localPosition.x > _rightLimit && speed > 0) {
        speed = speed * -1;
        Debug.Log(speed + " = speed. X = " + this.transform.localPosition.x);

    }
if(this.transform.localPosition.x<\u righlimit
&&this.transform.localPosition.x<\u leftLimit&&speed<0){
速度=速度*-1;
Debug.Log(speed+”=speed.X=“+this.transform.localPosition.X);
} 
//非常向右移动
如果(this.transform.localPosition.x>\u leftLimit
&&this.transform.localPosition.x>\u rightLimit&&speed>0){
速度=速度*-1;
Debug.Log(speed+”=speed.X=“+this.transform.localPosition.X);
}

这使得你的物体移动的方向只有在超过极限并且当前移动方向错误的情况下才会改变。

所以它实际上只是在相同的条件下来回切换速度,而不是在两种条件之间反弹。Doh,一双新鲜的眼睛。非常感谢。