繁体   English   中英

如何在unity3d中平稳地上下移动刚体对象

[英]How to move rigidbody object up or down smoothly in unity3d

我已经开始了一个项目,并在播放器上附加了刚体以对其施加一些力。因此,当我运行项目时,在FixedUpdate()函数中,我会向播放器施加力以使其向前移动。或右箭头执行“倾斜”表示旋转翅膀。 但是现在我想通过按Uparrow或Downarrow平稳地上下移动播放器,当我同时按下Uparrow和Downarrow时,这一定会对播放器产生影响。 这是我的代码。请帮助我。 :(

void FixedUpdate ()
{
  rb.AddForce(transform.forward *1000f);
  float movedir = Input.GetAxis ("Horizontal");

  Vector3 movement = new Vector3 (movedir, 0.0f, 0.0f);
  rb.velocity = movement * movingspeed;
  rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -3f);//perform tilt


 if (Input.GetKeyDown (KeyCode.UpArrow)) 
      { 
       //code for smoothly move up from current position to = current position + 2f 
      }

  if (Input.GetKeyDown (KeyCode.DownArrow)) 
      { 
        //code for smoothly move down from current position to = current position - 2f 
      }


}

您的问题还不清楚。 smoothly移动可能意味着很多事情。

通常,您应该使用RigidBody.MovePositionRigidbody.MoveRotation来设置Rigidbody的变换,而不是rb.rotationrb.position来获得“平滑”运动:

使用Rigidbody.MovePosition来移动Rigidbody,遵守Rigidbody的插值设置。

如果在Rigidbody上启用了Rigidbody插值,则调用Rigidbody.MovePosition会导致所渲染的任何中间帧中两个位置之间的平滑过渡。 如果要在每个FixedUpdate中连续移动刚体,则应使用此方法。

如果要将刚体从一个位置传送到另一个位置,而不渲染任何中间位置,请设置Rigidbody.position。

如您所见,根据使用Rigidbody.MovePosition的设置,可能已经导致“平滑”运动。

rb.MovePosition(transform.position + Vector3.up * 2.0f);

另外,您也使用Input.GetKeyDown因此它像触发器一样工作……它不会像Input.GetKey那样连续Input.GetKey

如果要在按住键的同时连续移动,请使用例如

// set e.g. in the inspector
public float verticalMoveSpeed;

// ...

if (Input.GetKey(KeyCode.UpArrow)) 
{ 
    rb.MovePosition(transform.position + Vector3.up * verticalMoveSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.DownArrow)) 
{ 
    rb.MovePosition(transform.position - Vector3.up * verticalMoveSpeed * Time.deltaTime);
}

如果您只想使用GetKeyDown触发移动,也可以执行例如

// set e.g. in the inspector
public float verticalMoveSpeed;

// ...

if (Input.GetKeyDown(KeyCode.UpArrow)) 
{ 
    StartCoroutine(MoveVertical(2.0f, verticalMoveSpeed));
} 
else if (Input.GetKeyDown(KeyCode.DownArrow)) 
{ 
    StartCoroutine(MoveVertical(-2.0f, verticalMoveSpeed));
}

// ...

private IEnumerator MoveVertical(float distance, float speed)
{
    var originalY = transform.position.y;
    var targetY = originalY + distance;

    var currentY = originalY; 
    do
    {     
        rb.MovePosition(new Vector 3(transform.position.x, currentY, transform.positiom.z);

        // Update currentY to the next Y position
        currentY = Mathf.Clamp(currentY + speed * Time.deltaTime, originalY, targetY);
        yield return null;
    }
    while(currentY < originalY);

    // make sure you didn't move to much on Y
    rb.MovePosition(new Vector3(transform.position.x, targetY, transform.position,z));
}

有两种方法可以防止并发例程:

  1. 使用一个标志。 这也可以防止例程蜂鸣被打断/两次调用/并发

     privtae bool isMovingVertical; // ... if (Input.GetKeyDown(KeyCode.UpArrow) && !isMovingVertical ) { StartCoroutine(MoveVertical(2.0f, verticalMoveSpeed)); } else if (Input.GetKeyDown(KeyCode.DownArrow) && !isMovingVertical ) { StartCoroutine(MoveVertical(-2.0f, verticalMoveSpeed)); } // ... private IEnumerator MoveVertical(float distance, float speed) { isMovingVertical = true; // ... isMovingVertical = false; } 
  2. 使用StopAllCoroutines中断正在运行的例程(注意,这可能会导致一个方向上的“无限”移动-至少在没有通过附加检查阻止它的情况下)

     if (Input.GetKeyDown(KeyCode.UpArrow)) { StopAllCoroutines(); StartCoroutine(MoveVertical(2.0f, verticalMoveSpeed)); } if (Input.GetKeyDown(KeyCode.DownArrow)) { StopAllCoroutines(); StartCoroutine(MoveVertical(-2.0f, verticalMoveSpeed)); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM