unity-game-engine2dcollision

Unity 2D Trail Renderer Collision


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:

enter image description here

Is there some script I can write to achieve my goal? Or does Unity Engine have a render solution?

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

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

enter image description here


Solution

  • 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);