C# 单位为2dc的X速度#
我不熟悉C#和Unity。我需要标准化我的球员的速度,但只在x方向。我现在的代码是C# 单位为2dc的X速度#,c#,unity3d,C#,Unity3d,我不熟悉C#和Unity。我需要标准化我的球员的速度,但只在x方向。我现在的代码是 void Update () { if(rb2d.velocity.magnitude > maxSpeed) { rb2d.velocity = rb2d.velocity.normalized * maxSpeed; } 这控制了所有的速度。我试过了 void Update () { if(rb2d.velocity.x.magnitude > maxSpeed)
void Update ()
{
if(rb2d.velocity.magnitude > maxSpeed)
{
rb2d.velocity = rb2d.velocity.normalized * maxSpeed;
}
这控制了所有的速度。我试过了
void Update ()
{
if(rb2d.velocity.x.magnitude > maxSpeed)
{
rb2d.velocity.x = rb2d.velocity.x.normalized * maxSpeed;
}
但我认为这是在Unity 5中删除的。我能做什么?只需将速度X除以速度的大小,这基本上就是normalize所做的
// Make a copy of the current velocity.
Vector2 velocity = rb2d.velocity;
// Divide the x component by magnitude, equivalent to normalizing it.
velocity.x = velocity.x / velocity.magnitude;
// velocity now only has its x component normalized and y untouched
rb2d.velocity = velocity * maxSpeed;
只需将速度X除以速度的大小,这基本上就是normalize所做的
// Make a copy of the current velocity.
Vector2 velocity = rb2d.velocity;
// Divide the x component by magnitude, equivalent to normalizing it.
velocity.x = velocity.x / velocity.magnitude;
// velocity now only has its x component normalized and y untouched
rb2d.velocity = velocity * maxSpeed;
我不太懂团结,但我懂数学。您可以通过将x和y分量除以向量的长度来规范化向量,我尝试过这样做,但这并不是真正的问题,主要是因为最大速度是问题所在。谢谢,我不太懂团结,但我懂数学。您可以通过将x和y分量除以向量的长度来规范化向量,我尝试过这样做,但这并不是真正的问题,主要是因为最大速度是问题所在。但我不完全理解我如何实现这一点。我把它写在if语句中。它说XVelocity在当前环境中不存在。@Zanolon好吧,
XVelocity
在这个答案中也不存在,所以这显然是您添加的内容。我不完全理解如何实现这一点。我把它写在if语句中。这是说XVelocity在当前上下文中不存在。@Zanolon好吧,XVelocity
在这个答案中也不存在,所以它显然是你添加的东西。