Unity:协作程序中的无限While循环
本文关键字:无限 While 循环 协作 程序 Unity | 更新日期: 2023-09-27 18:13:22
好的,所以我试图为我的2d字符创建一个小的破折号协程。当协同程序调用时,重力关闭,它在一段时间内在两种速度之间跳跃。问题是在我的Dash协程中,while循环检查时间。时间(当前时间)>开始时间+冲刺时间
在与Mono调试时,我发现我的变量currentTime在设置后没有改变,即使我可以清楚地看到While循环运行不止一次。这让我陷入了一个无限循环。
有什么建议吗?
void Update () {
MoveAndJump ();
CheckDash ();
}
void MoveAndJump(){
if(dashing){
return;
}
Vector2 moveDir = new Vector2 (Input.GetAxisRaw ("Horizontal") * moveSpeed, rb.velocity.y);
rb.velocity = moveDir;
// Consider Switching to an overlap circle based on the actual character graphic. This currently uses a rectangle
// This can also use 4 points to create a more complex box shape. This only works well when using at least about 1 unit size. any smaller and it is innacurate
isGrounded = Physics2D.OverlapArea (groundPoint_right.position, groundPoint_left.position, groundMask);
Debug.Log (isGrounded);
if (Input.GetAxisRaw ("Horizontal") == 1) {
transform.localScale = new Vector3 (1 , 1 * transform.localScale.y, 1* transform.localScale.z);
} else if (Input.GetAxisRaw ("Horizontal") == -1) {
transform.localScale = new Vector3 (-1, 1* transform.localScale.y, 1* transform.localScale.z);
}
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce (new Vector2 (0, jumpHeight));
}
}
void CheckDash(){
if(Input.GetKeyDown(KeyCode.W) && !dashing && Time.time > nextdashtime){
dashing = true;
StartCoroutine ("Dash");
}
}
IEnumerator Dash(){
//Before dash loop
rb.gravityScale = 0f;
float startDashTime = Time.time;
float endDashTime = startDashTime + dashDuration;
float currentDashTime;
//Dash Loop
while(Time.time < (startDashTime + dashDuration)){
currentDashTime = Time.time;
// The value to lerp by should be the % that time.time is between start time and end time
rb.velocity = new Vector2 (Mathf.Lerp (startDashSpeed, endDashSpeed, ((currentDashTime - startDashTime) / (endDashTime - startDashTime))),0f);
}
//When dash loop is complete
nextdashtime = Time.time + dashcooldown;
dashing = false;
rb.gravityScale = 1;
yield return null;
}
// FOR CHECKING GROUND CHECK LIMITS
/*void OnDrawGizmos(){
Gizmos.color = Color.red;
Gizmos.DrawLine (groundPoint_left.position, groundPoint_right.position);
}*/
}
你不需要所有这些刷新 !
如果使用刚体,则可以使用以下代码:
using UnityEngine;
public class CubeController : MonoBehaviour
{
private Rigidbody _rigidbody;
public float Force = 10;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
ForceMode? forceMode = null;
if (Input.GetKeyDown(KeyCode.W)) // normal
forceMode = ForceMode.Force;
else if (Input.GetKeyDown(KeyCode.S)) // dash
forceMode = ForceMode.Impulse;
if (forceMode.HasValue)
_rigidbody.AddRelativeForce(Vector3.forward*Force, forceMode.Value);
}
}
这是一个使用3D的例子,但你有相同的2D: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
步骤:
- 将刚体组件添加到地面,将其设置为运动学
- 为播放器添加刚体组件
- 使用
Rigidbody2D
类为您的播放器绘制上述组件的草图 - 根据需要调整参数
- 你现在应该有一个简单而健壮的样式:)
我在while循环中忘记了yield return null,这使我陷入了无限循环。
while(Time.time < (startDashTime + dashDuration)){
currentDashTime = Time.time;
// The value to lerp by should be the % that time.time is between start time and end time
rb.velocity = new Vector2 (Mathf.Lerp (startDashSpeed, endDashSpeed, ((currentDashTime - startDashTime) / (endDashTime - startDashTime))),0f);
yield return null;
}