英文:
How to set the distance my character can dash?
问题
我正在尝试为我的角色添加冲刺机制,但我想设置它可以冲刺的距离。目前它可以正常工作,但我想让它感觉更好。在冲刺时,它会冻结y轴位置,并移除碰撞,所以你可以穿过墙壁/避免受到伤害。唯一的问题是,它的冲刺取决于速度,这意味着如果我提高速度,角色会冲得更远。
我应该如何限制冲刺的距离,以便如果我增加冲刺速度,它只会在那个距离内更快地移动,而不是更远?
以下是我的代码:
public Rigidbody2D rb;
private BoxCollider2D coll;
//冲刺变量
[SerializeField] private float dashDuration = 0.5f;
[SerializeField] private float dashSpeed = 15.0f;
public void Dash()
{
StartCoroutine(DashRoutine());
}
IEnumerator DashRoutine()
{
movementAllowed = false;
coll.isTrigger = true;
rb.constraints = RigidbodyConstraints2D.FreezePositionY; //冲刺时冻结y轴位置
if(rb.velocity.x < 0)
{
rb.velocity = new Vector2(-dashSpeed, transform.position.y);
}
else
{
rb.velocity = new Vector2(dashSpeed, transform.position.y);
}
Debug.Log("我在冲刺!");
yield return new WaitForSeconds(dashDuration);
rb.constraints = RigidbodyConstraints2D.None;
coll.isTrigger = false;
movementAllowed = true;
}
希望这有助于你解决问题。
英文:
I'm trying to give my character the mechanic of dashing but I want to set the distance it can dash. Right now it works fine but I want it to feel nicer. It freezes the y position while dashing, and it removes collisions so you can dash through walls/avoid incoming damage. The only problem is that it dashes depending on speed, which means if I make the speed higher, the character dashes further away.
How should I go about restricting the length of the dash so that if I increase the dash speed it just goes faster within that distance, rather than further?
Here is my code:
public Rigidbody2D rb;
private BoxCollider2D coll;
//Dashing variables
[SerializeField] private float dashDuration = 0.5f;
[SerializeField] private float dashSpeed = 15.0f;
public void Dash()
{
StartCoroutine(DashRoutine());
}
IEnumerator DashRoutine()
{
movementAllowed = false;
coll.isTrigger = true;
rb.constraints = RigidbodyConstraints2D.FreezePositionY; //Freeze y position while dashing
if(rb.velocity.x < 0)
{
rb.velocity = new Vector2(-dashSpeed, transform.position.y);
}
else
{
rb.velocity = new Vector2(dashSpeed, transform.position.y);
}
Debug.Log("Im dashing!");
yield return new WaitForSeconds(dashDuration);
rb.constraints = RigidbodyConstraints2D.None;
coll.isTrigger = false;
movementAllowed = true;
}
答案1
得分: 2
速度、距离、时间公式:
速度 = 距离 / 时间
距离 = 速度 * 时间
时间 = 距离 / 速度
上下文中:
冲刺速度 = 冲刺距离 / 冲刺持续时间
冲刺距离 = 冲刺速度 * 冲刺持续时间
冲刺持续时间 = 冲刺距离 / 冲刺速度
为了保持冲刺距离不变但增加速度,您需要减少冲刺时间。您可以通过解决时间公式 T= D/S
来确定冲刺时间。
例如,冲刺持续时间 = 冲刺距离 / 冲刺速度
。
英文:
Speed, Distance, Time formulae:
S = D / T
D = S * T
T = D / S
In Context:
dashSpeed = dashDistance / dashDuration
dashDistance = dashSpeed * dashDuration
dashDuration = dashDistance / dashSpeed
To keep the distance of the dash the same while increasing speed, you need to decrease the dash time. You can determine the dash time by solving the formula for time T=D/S
eg. dashDuration = dashDistance / dashSpeed
答案2
得分: 0
Here is the translation of the provided content:
与这个很好的答案不同的是:
而不是等待固定的dashDuration
,您可以根据行进的距离(或两者兼顾,停止其中一个先完成)
...
[SerializedField]
private float maxDashDistance;
...
private IEnumerator DashRoutine()
{
...
Debug.Log("我在冲刺!");
var startPosition = rb.position;
// 比较平方幅度稍微便宜一些
var sqrMaxDistance = maxDashDistance * maxDashDistance;
// 循环,直到dashDuration或maxDashDistance中的任何一个被超越
for (var timePassed = 0f;
timePassed < dashDuration && (rb.position - startPosition).sqrMagnitude < sqrMaxDistance;
timePassed += Time.deltaTime)
{
yield return null;
}
rb.constraints = RigidbodyConstraints2D.None;
coll.isTrigger = false;
// 假设这确保了您在dashSpeed停止移动
movementAllowed = true;
}
或者,如果for
循环的条件对您来说看起来太难看,您也可以像这样拖拽它:
for (var timePassed = 0f; timePassed < dashDuration; timePassed += Time.deltaTime)
{
if((rb.position - startPosition).sqrMagnitude >= sqrMaxDistance) break;
yield return null;
}
英文:
Alternatively to this great answer:
Well instead of waiting a fixed dashDuration
you can rather go by the distance traveled (or combine both and stop dashing on whichever finishes first)
...
[SerializeField]
private float maxDashDistance;
...
private IEnumerator DashRoutine()
{
...
Debug.Log("Im dashing!");
var startPosition = rb.position;
// it is slightly cheaper to compare square magnitude
var sqrMaxDistance = maxDashDistance * maxDashDistance;
// iterates until either the dashDuration or the maxDashDistance is exceeded
for (var timePassed = 0f;
timePassed < dashDuration && (rb.position - startPosition).sqrMagnitude < sqrMaxDistance;
timePassed += Time.deltaTime)
{
yield return null;
}
rb.constraints = RigidbodyConstraints2D.None;
coll.isTrigger = false;
// assuming this ensures you stop moving with the dashSpeed
movementAllowed = true;
}
or if the for
loop condition looks too ugly to you you can also drag it in like
for (var timePassed = 0f; timePassed < dashDuration; timePassed += Time.deltaTime)
{
if((rb.position - startPosition).sqrMagnitude >= sqrMaxDistance) break;
yield return null;
}
答案3
得分: -1
希望我能帮上忙 问题在于你只是设置了刚体的速度;因此,如果速度更大,物体会移动更远。更好的方法是以某种方式平滑地从当前位置移动到所需距离;换句话说,我们可以进行线性插值或lerp。你可以阅读文档。你可以使用刚体的位置作为起始位置,然后通过添加一些向量(距离)到当前位置来获取结束位置。
英文:
Hopefully I can help out The issue is that you're simply setting the velocity of the rigidBody; Thus, if the velocity is greater, the object will travel further. A better approach would be to somehow smoothly move from the current position to a desired distance; In other words, we could linearly interpolate, or lerp. You can read the documentation. You can use the rigidbody's position for the start position, and then get the end position by adding the current position with some vector (distance).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论