U3D里实现角色之间在行走过程中防止重叠

来源:互联网 发布:淘宝店怎么提升流量 编辑:程序博客网 时间:2024/05/16 00:49

一开始以为要自己手动写代码在寻径时绕开障碍物重新计算路径,后来放弃,想到用添加刚体与碰撞盒,但被碰撞后击飞了,于是再添加物理材质属性组件设置Bounciness反弹属性,还是存在很多抖动,最后是用角色控制器组件Character Controller,然后再修改角色更新位置的方法即可.


角色的更新位置不能直接直接修改位置值,而需要如下方式:

normalMoveVector = (positionTo - gameObject.transform.localPosition).normalized * speed;
CharacterController character_controller = GetComponent<CharacterController>();
character_controller.Move(normalMoveVector * Time.deltaTime);


具体Character Controller的设置面板可以参考如下:

Properties 属性


Height
高度
The Character's Capsule Collider height. Changing this will scale the collider along the Y axis in both positive and negative directions.
角色的胶囊碰撞器高度。改变其大小会使碰撞器在Y轴方向3两端伸缩。
Radius
半径
Length of the Capsule Collider's radius. This is essentially the width of the collider.
胶囊碰撞器的半径长度。级碰撞器的宽度。
Slope Limit
坡度限制
Limits the collider to only climb slopes that are equal to or less than the indicated value.
限制碰撞器只能爬小于等于该值的斜坡。
Step Offset
台阶高度
The character will step up a stair only if it is closer to the ground than the indicated value.
角色可以迈上的最高台阶高度。
Min Move Distance
最小移动距离
If the character tries to move below the indicated value, it will not move at all. This can be used to reduce jitter. In most situations this value should be left at 0.
如果角色移动的距离小于该值,那角色就不会移动。这可以避免颤抖现象。大部分情况下该值被设为0。
Skin width
皮肤厚度
Two colliders can penetrate each other as deep as their Skin Width. Larger Skin Widths reduce jitter. Low Skin Width can cause the character to get stuck. A good setting is to make this value 10% of the Radius.
皮肤厚度决定了两个碰撞器可以互相渗入的深度。较大的皮肤厚值度会导致颤抖。小的皮肤厚度值会导致角色被卡住。一个合理的设定是使该值等于半径(Radius)的10%。
Center
中心
This will offset the Capsule Collider in world space, and won't affect how the Character pivots.
该值决定胶囊碰撞器在世界空间中的位置,并不影响角色的行动。


Details 细节


The traditional Doom-style first person controls are not physically realistic. The character runs 90 miles per hour, comes to a halt immediately and turns on a dime. Because it is so unrealistic, use of Rigidbodies and physics to create this behavior is impractical and will feel wrong. The solution is the specialized Character Controller. It is simply a capsule shaped Collider which can be told to move in some direction from a script. The Controller will then carry out the movement but be constrained by collisions. It will slide along walls, walk up stairs (if they are lower than the Step Offset) and walk on slopes within the Slope Limit.


传统的"毁灭战士"风格的第一人称控制并未依据物理现实。角色跑到了90英里每小时,然后马上停下而且可以极快的转身。因为是这么地不真实,用刚体和物理效果来创建这种行为是不切实际的,感觉上也不对劲。解决办法就是专门的角色控制器。很简单,就是一个胶囊碰撞器附加了可以控制其移动的脚本。控制器会执行脚本传达的动作但被碰撞影响。它会沿着墙动,走上台阶(如果台阶高度低于Step Offset属性)以及走上坡度小于Slope Limit的斜坡。


The Controller does not react to forces on its own and it does not automatically push Rigidbodies away.

控制器不会对加在它自身上的力做出反应,也不会自动推开其他刚体。

If you want to push Rigidbodies or objects with the Character Controller, you can apply forces to any object that it collides with via the OnControllerColliderHit() function through scripting.

如果想让角色控制器推开其他刚体或者对象,你可以在对象附加的脚本中添加OnControllerColliderHit()函数,这样对它们施加力就能够产生碰撞。


On the other hand, if you want your player character to be affected by physics then you might be better off using a Rigidbody instead of the Character Controller.

另外,如果你想让你的游戏角色被物理效果影响,那就最好使用刚体而不是角色控制器。

Fine-tuning your character 仔细调整你的角色

You can modify the Height and Radius to fit your Character's mesh. It is recommended to always use around 2 meters for a human-like character. You can also modify the Center of the capsule in case your pivot point is not at the exact center of the Character.

你可以修改Height 和Radius的大小来使控制器和你的角色网格匹配。对于人形的角色一直推荐使用大约2米。同样可以修改胶囊的Center属性来使转动中心和角色的中心吻合。


Step Offset can affect this too, make sure that this value is between 0.1 and 0.4 for a 2 meter sized human.

Step Offset属性也会影响角色,确保对于一个两米大小的人来说该值介于0.1到0.4之间。

Slope Limit should not be too small. Often using a value of 90 degrees works best. The Character Controller will not be able to climb up walls due to the capsule shape.

Slope Limit不能设的太小,一般使用90度会比较合适。由于是胶囊体,角色控制器不会爬上墙。

Don't get stuck 不要被卡住

The Skin Width is one of the most critical properties to get right when tuning your Character Controller. If your character gets stuck it is most likely because your Skin Width is too small. The Skin Width will let objects slightly penetrate the Controller but it removes jitter and prevents it from getting stuck.

Skin Width是调整你的角色控制器时最难正确设置的边界值之一。如果你的角色被卡住了,那最有可能就是因为该值设的太小。Skin Width会让其他物体有少许穿透控制器但是能够避免震颤以及阻止被卡住。

It's good practice to keep your Skin Width at least greater than 0.01 and more than 10% of the Radius.

实践中比较好的值是至少要大于0.01并且大于Radius的10%。

We recommend keeping Min Move Distance at 0.

推荐把Min Move Distance设为0.

Hints 提示


Try adjusting your Skin Width if you find your character getting stuck frequently. 
如果发现你的角色频繁被卡住,尝试调整Skin Width值。
The Character Controller can affect objects using physics if you write your own scripts. 
角色控制器可以使用物理效果影响其他对象,前提是你自己写了脚本。
The Character Controller can not be affected by objects through physics. 
角色控制器不能通过物理效果被其他对象影响。

0 0