【问题标题】:How to move the object (two-dimensional) based on its angle?如何根据角度移动对象(二维)?
【发布时间】:2020-01-11 13:36:41
【问题描述】:

我正在研究unity中的一些细节,结果我陷入了死胡同。首先我想知道,是否可能有一个功能,但是如何根据它所看到的角度来提高物体的速度。请您用 examples 进行演示,因为到目前为止我的想法很枯燥。我尝试使用数学来实现这个功能(可能是个坏主意),因为我很想知道这种情况在数学实践中是如何运作的,因此开发了一些关于它的理论:

首先我想,在90度角度,一次移动,只有Y轴增加,所以X轴 保持在 0。所以要找出给定角度的 X 和 Y 的确切变化,您所要做的就是找到角度割线并增加它,还是在另一个点?可能我的想法是错误的,我是三角学的“新手”,但我很想知道我错了什么,如果我错了,什么是正确的想法。

如果我说错了很多事情,请告诉我,因为我知道我需要多学习:)

下面是我创建的函数的代码,最初只是让我的角色在 Y 轴上旋转,所以只需增加或减少速度值。所以我添加了一个方法,根据物体正在看的角度生成随机角度,所以我只是使用了不同的象限。

void invertMove()
{
    if(!inverse)
    {
        float tempRot = Random.Range(180f, 360f);
        zombieSpeed = -0.01f;
        zombieBody.SetRotation(tempRot);
        inverse = true;
    }
    else
    {
        float tempRot = Random.Range(0f, 180f);
        zombieSpeed = 0.01f;
        zombieBody.SetRotation(tempRot);
        inverse = false;

    }
}

【问题讨论】:

  • 不太确定SetRotation 是什么,但是如果您想生成一个以度为单位的随机角度,那么您可以通过将向右的单位向量逆时针旋转该角度来获得 x/y 坐标,那么您可以做float randomAngle = Random.Range(0f, 360f);Vector2 coords = new Vector2(Mathf.Cos(randomAngle * Mathf.Deg2Rad), Mathf.Sin(randomAngle * Mathf.Deg2Rad));
  • @Ruzihm SetRotation 似乎来自Rigidbody2D ;)

标签: c# unity3d math


【解决方案1】:

我们不知道zombieSpeed 做了什么,但假设您在这里谈论的是Rigidbody2D.SetRotation,您可能想分配一个新的velocity

velocity 位于全局空间。你不能使用例如transform.right 作为新方向,因为transform 尚未更新.. 只有zombieBody 是.. 但您可以使用Rigidbody2D.GetRelativeVector 在旋转后设置新的本地方向

rigidBody 局部空间中给定矢量relativeVector,获取一个全局空间矢量

// or Vector2.up depending on your setup
zombieBody.velocity = zombieBody.GetRelativeVector(Vector2.right * 0.01f);

因此,根据您的需要,您不会区分正速度和负速度,而是留在0.01f 并使用

  • 用于创建一个新的完全随机的方向

    void newRandomDirection()
        float newRot = Random.Range(0f, 360f);
        zombieBody.SetRotation(newRot);
        zombieBody.velocity = zombieBody.GetRelativeVector(Vector2.right * 0.01f);
    }
    
  • 实际上你可以简单地反转方向

    void invertMove()
    {
        var newRot = zombieBody.rotation + 180f;
        // optionally you can always fix it to a value 0-360 but that's not really required
        //if(newRot > 360f)
        //{
        //    newRot -= 360f;
        //}
        zombieBody.SetRotation(newRot);
        zombieBody.velocity = zombieBody.GetRelativeVector(Vector2.right * 0.01f);
    }
    
  • 或者,如果您想要一个新的随机方向,介于 -90(== +270) 到 90 度之间,我宁愿使用当前方向

    void invertMoveHemiSphere()
    {
        var newRot = zombieBody.rotation + Random.Range(90f, 270f);
        // optionally you can always fix it to a value 0-360 but that's not really required
        //if(newRot < 0f)
        //{
        //    newRot += 360f;
        //} 
        //else if(newRot > 360f)
        //{
        //    newRot -= 360f;
        //}
        zombieBody.SetRotation(newRot);
        zombieBody.velocity = zombieBody.GetRelativeVector(Vector2.right * 0.01f);
    }
    
  • 或坚持你到目前为止所做的事情

    void invertMove()
    {
        inverse = !inverse;
    
        float newRot = inverse? Random.Range(0f, 180f) : Random.Range(180f, 360f);
        zombieBody.SetRotation(newRot);
        zombieBody.velocity = zombieBody.GetRelativeVector(Vector2.right * 0.01f);   
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多