IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]unity不使用刚体的碰撞检测

    u010019717发表于 2015-08-16 11:01:48
    love 0

    孙广东  2015.8.15


    目的: 这篇文章的主要目的是为了让您了解有关于在Unity内如何使用没有刚体的碰撞检测。
    在这里,检查的只是垂直运动方向,但可以通过修改脚本,将对象添加到场景检查其他方向 (或更改cube 的位置。


    unity就是 3D 的游戏引擎,带有内置物理Nvidia PhysX。游戏对象应用物理模拟必须具有刚体。它常用于碰撞检测。
             假设我们只想要碰撞检测模拟物理学,然后只对碰撞检测使用物理引擎可能会降低整体性能。
    有两个解决办法:
    解决办法-1: 使刚体运动学和捕获触发器事件。
    解决方案-2: 不使用的刚体 (可能是最好的解决办法)!!!   使用 Raycast 是一个很好的选择。
             你可以投数以百计的每帧的光线而多降低性能。这是(提供)一种casting来源射线的方向,然后确定光线如果投射与任何碰撞器的方法。我们可以使用这个处理碰撞检测,通过casting射线在 x 和 y 轴,以得到确认的游戏物体的周围环境。
    我们将按照下面给出的步骤:
               1、获得游戏对象的方向。
                2、投射光线。
               3、从命中结果确定碰撞体。
    简单的 raycast 代码看起来像这样:
    if (Physics.Raycast(transform.position, Vector3.forward, 10))
    print("There is something in front of the object!");


    在这里,第一个参数是射线的原点,第二个参数是射线的方向和第三个参数是射线的长度。Unity允许获取结果,
    RaycastHit hitInfo;
    if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
    print(“There is something ” + hitInfo.distance +”m away from gameobject”);


    例如,让我们使Cube,当它与另一个Cube相撞的可以回去。

    1.  3个Cube,如下图所示。




    添加 Box Collider。将 CollisionDetector 脚本添加到中间的Cube,放置在其他两个Cube (在上面的图像中选取) 之间的。在 CollisionDetector 脚本中,使之Cube与上方或下方的碰撞 就相反。
    CollisionDetector.cs
    using UnityEngine;
    using System.Collections;
    public class CollisionDetector : MonoBehaviour
    {
    public float MovingForce;
    Vector3 StartPoint;
    Vector3 Origin;
    public int NoOfRays = 10;
    int i;
    RaycastHit HitInfo;
    float LengthOfRay, DistanceBetweenRays, DirectionFactor;
    float margin = 0.015f;
    Ray ray;
    void Start ()
    {
    //Length of the Ray is distance from center to edge
    LengthOfRay = collider.bounds.extents.y;
    //Initialize DirectionFactor for upward direction
    DirectionFactor = Mathf.Sign (Vector3.up.y);
    }
     
    void Update ()
    {
    // First ray origin point for this frame
    StartPoint = new Vector3 (collider.bounds.min.x + margin,transform.position.y,transform.position.z);
    if (!IsCollidingVertically ()) {
    transform.Translate (Vector3.up * MovingForce * Time.deltaTime * DirectionFactor);
    }
    }
     
    bool IsCollidingVertically ()
    {
    Origin = StartPoint;
    DistanceBetweenRays = (collider.bounds.size.x - 2 * margin) / (NOofRays - 1);
    for (i = 0; i<NOofRays; i++) {
    // Ray to be casted.
    ray = new Ray (Origin, Vector3.up * DirectionFactor);
    //Draw ray on screen to see visually. Remember visual length is not actual length.
    Debug.DrawRay (Origin, Vector3.up * DirectionFactor, Color.yellow);
    if (Physics.Raycast (ray, out HitInfo, LengthOfRay)) {
    print ("Collided With " + HitInfo.collider.gameObject.name);
    // Negate the Directionfactor to reverse the moving direction of colliding cube(here cube2)
    DirectionFactor = -DirectionFactor;
    return true;
    }
    Origin += new Vector3 (DistanceBetweenRays, 0, 0);
    }
    return false;
     
    }
    }

    解释
    在这里 NoOfRays 和Moving force 是公共变量,所以它可以在运行时根据需要改变了。请确保移动速度不超过该Cube在顶部和底部之间的距离。




    DirectionFactor 成倍增加的运动力和射线的方向,因为它用来决定的方向。最初,它设置为向上 (正 y 轴) 方向。尽快移动相撞其他Cube的Cube被扭转这个方向决胜局。通过改变方向矢量,可以根据要求改变方向。DirectionFactor 仅用于反转方向。














沪ICP备19023445号-2号
友情链接