且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Unity 2D Trail Renderer 碰撞

更新时间:2022-10-14 19:37:25

我的做法如下:

创建一个脚本并将其附加到路径跟随的对象上.

使用碰撞器大小创建一个空游戏对象的预制件,并将其附加到脚本中.

public TrailRenderer 路径;//轨迹公共游戏对象 TrailFollower;公共游戏对象碰撞器预制件;

创建一个碰撞器预制池(使用越多成本越高,但更准确.)

public int poolSize=5;游戏对象[]池;无效开始(){trail = GetComponent();池 = 新游戏对象 [池大小];for (int i = 0; i < pool.Length; i++){pool[i] = 实例化(ColliderPrefab);}}

现在在更新游戏时,您应该执行以下操作:

 void Update() {如果 (!trail.isVisible){for (int i = 0; i < pool.Length; i++){pool[i].gameObject.SetActive(false);}}别的{轨迹碰撞();}}void TrailCollission(){for (int i = 0; i < pool.Length; i++){if (pool[i].gameObject.activeSelf == false){pool[i].gameObject.SetActive(true);pool[i].gameObject.transform.position = TrailFollower.gameObject.transform.position;返回;}}}

  1. 检查是否在屏幕上绘制轨迹,如果没有,隐藏所有碰撞器.

  2. 否则,在游泳池上奔跑并搜索隐藏的碰撞器.

  3. 当发现隐藏的碰撞器时,让它在跟踪游戏对象的位置可见.

(如果不是所有的踪迹都立即消失,您还可以添加 iEnumerator 以在所需时间后将其隐藏).

通过使池变大,丢失对撞机的机会将降低,玩弄它,直到找到适合您需要的东西.

要让碰撞器在一段时间后隐藏,请执行以下操作:

private IEnumerator hide(float waitTime, GameObject p){而(真){yield return new WaitForSeconds(waitTime);p.SetActive(false);产量中断;}产量中断;}

在设置他们的位置后调用这个

hide(time,pool[i].gameObject);启动协程(隐藏);

I making 2D unity game but I am facing a major issue which my game depends on.

I attached a trail renderer component to my player and what I need is to make the renderer be a collider act as a MeshCollider I just didn't figure out if it is possible to make a collider to take the shape of a 2D trail renderer.

I've searched over google but didn't have a well performing solution:

  • Some say create an empty gameobject attach the trail renderer component then add a collider to it. But doesn't work neither.
  • I tried to follow this WIKI and My Trail Renderer collides but is not nice but I need to assign the tag to the trail too.

Is there some script I can write to achieve my goal Or UnityEngine has a render solution.Thanks in advance.

EDIT1:

After I copied the script and run it The trail collides but it acts goofy when player is not moving.

EDIT2:

The game basically is a player that has transform.position equal to the mouse position.So the trail doesn't have a specific length.

The way I would do it is the following:

Create a script and attach it to the object that the trail follows.

Create a prefab of empty gameObject with collider the size of your trail and attach it to the script.

public TrailRenderer trail; //the trail
public GameObject TrailFollower;
public GameObject ColliderPrefab;

Create a pool of the collider prefab (the more you use the more expensive it is, but more accurate.)

public int poolSize=5;
GameObject[] pool;

void Start()
{
    trail = GetComponent<TrailRenderer>();
    pool = new GameObject[poolSize];
    for (int i = 0; i < pool.Length; i++)
    {
        pool[i] = Instantiate(ColliderPrefab);
    }
}

now while updating the game you should do the following:

 void Update () {
    if (!trail.isVisible)
    {
        for (int i = 0; i < pool.Length; i++)
        {
            pool[i].gameObject.SetActive(false);

        }
    }
    else
    {
        TrailCollission();
    }

}

void TrailCollission()
{
    for (int i = 0; i < pool.Length; i++)
    {
        if (pool[i].gameObject.activeSelf == false)
        {
            pool[i].gameObject.SetActive(true);
            pool[i].gameObject.transform.position = TrailFollower.gameObject.transform.position;
            return;
        }
    }
}

  1. check if trail is being drawn on screen, if not, hide all colliders.

  2. else, run on the pool and search for hidden collider.

  3. when found hidden collider make it visible on the position of the trail gameObject.

(if not all the trail disappear at once you can also add iEnumerator that will hide it after the required time).

By making the pool bigger the chance for missing colliders will be lowered, play around with it until you find something that suits your needs.

edit:

To make Colliders hide after some time do this:

private IEnumerator hide(float waitTime, GameObject p)
{
    while (true)
    {
        yield return new WaitForSeconds(waitTime);
        p.SetActive(false);

        yield break;
    }
    yield break;
}

call this after setting their position

hide(time,pool[i].gameObject);
StartCoroutine(hide);