简体   繁体   English

确定统一旋转角色的方向?

[英]Determining which direction to rotate a character in unity?

I am currently having a lot of issues trying to determine which direction is the shortest path between two angles.我目前在尝试确定哪个方向是两个角度之间的最短路径时遇到了很多问题。 I am programming a boid in Unity2D, and I am working on the separation aspect.我正在 Unity2D 中编写一个 boid,并且我正在研究分离方面。 For context, my current system works by creating a specified amount of gameobjects around the boid (I found that 16 of these work nicely).就上下文而言,我当前的系统通过在 boid 周围创建指定数量的游戏对象来工作(我发现其中 16 个工作得很好)。 These gameobjects have their own scripts which draws a linecast to the parent boid, and records it's angle to the player.这些游戏对象有自己的脚本,可以将 linecast 绘制到父 boid,并记录它与玩家的角度。 If the linecast hits a wall, or a boid, it adds the cosine and sine of it's angle to two separate lists that are stored in the parent boid.如果 linecast 撞到墙壁或 boid,它会将其角度的余弦和正弦添加到存储在父 boid 中的两个单独列表中。 The parent boid calculates the average of these two lists and creates a vector based on that.父 boid 计算这两个列表的平均值并基于此创建一个向量。 The boid gets the angle to that vector, and adds 180 degrees to that to find the direction it needs to head in in order to not hit the object. boid 获得该矢量的角度,并将其添加 180 度以找到它需要前进的方向,以免撞到 object。 As horrifically inefficient it may sound, it was the only way I could think to do this, and works well.听起来效率极低,但这是我能想到的唯一方法,而且效果很好。

Now, what doesn't work is rotating the boid towards that goal direction.现在,不起作用的是向那个目标方向旋转身体。 I do not want the boid to instantly lock into position, but to slowly rotate.我不希望 boid 立即锁定到 position,而是缓慢旋转。 I want this because it adds a lot of variation to the boid's movement.我想要这个,因为它为身体的运动增加了很多变化。 The movement takes the current direction that the boid is facing and moves that way.运动采用 boid 所面对的当前方向并以该方向移动。 For the most part, I got this to work with this code:在大多数情况下,我得到了这个代码:

void Rotate()
    {
        if (currentRotation != goalRotation)
        {
            int sign = (currentRotation < goalRotation) ? 1 : -1;
            rotateSpeed = Mathf.Abs(currentRotation - goalRotation) / 10;
            currentRotation += rotateSpeed * sign;
            if (Mathf.Abs(currentRotation - goalRotation) <= .5) //If the current rotation is close, just make it the goal rotation.
            {
                currentRotation = goalRotation;
            }
        }
    }

(Angles are based off the unit circle) (角度基于单位圆)

I really like how this starts the boid rotating fast, and slows down as the boid reaches the desired angle.我真的很喜欢它如何开始快速旋转 boid,并在 boid 达到所需角度时减慢速度。 So, this code works, except when the current rotation is something like 350 degrees, and the goal direction is something like 10 degrees.因此,此代码有效,除非当前旋转是 350 度,而目标方向是 10 度。 The boid will rotate clockwise, even though the shortest path is counterclockwise.即使最短的路径是逆时针的,boid 也会顺时针旋转。 I know exactly why, but have no clue how to fix it.我确切地知道为什么,但不知道如何解决它。 Any help would be appreciated.任何帮助,将不胜感激。

I don't understand what boid is, but here's the solution:我不明白boid是什么,但这是解决方案:

Not smooth不光滑

transform.LookAt(target);

Smooth光滑的

var targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

You can combine Quaternion.RotateTowards and Quaternion.Slerp to achieve ultra smooth solution.您可以结合Quaternion.RotateTowardsQuaternion.Slerp来实现超平滑的解决方案。

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

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