3

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.

enter image description here

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.

enter image description here

Bak Stak
  • 127
  • 3
  • 16
  • http://wiki.unity3d.com/index.php/TrailRendererWith2DCollider – SpoocyCrep Jul 26 '18 at 06:15
  • @SpoocyCrep I tried this script but as I mentioned It didn't get to work with my game. – Bak Stak Jul 26 '18 at 06:18
  • the trail follows the circle you drew right? how long the trail appears on screen? – SpoocyCrep Jul 26 '18 at 06:22
  • Ok I think it didn't work because I assigned the script to an empty child object of the player I will try to assign the script to the player then see what happens – Bak Stak Jul 26 '18 at 06:26
  • "create an empty gameobject attach the trail renderer component" is the suggested way because they say the only renderer should be trail renderer on the game object – canbax Aug 25 '19 at 09:25

2 Answers2

3

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);
SpoocyCrep
  • 604
  • 7
  • 23
  • I am still a beginner in unity so Pooling is a bit new thing to me I've understood the main idea of pooling but for now I have to learn more about it because I think it might be the solution. – Bak Stak Jul 26 '18 at 07:04
  • That looks meaningful but in my case the trail length is variable so i can not set the `poolSize ` a specific number. – Bak Stak Jul 26 '18 at 07:32
  • can you explain more how the trail works in your game? what do you mean in variable? – SpoocyCrep Jul 26 '18 at 07:33
  • I'm sorry, but the questions are important for me to be able to understand and help you. You say the trail doesn't have a fixed length and its okay and to be expected, after how much time it disappears\does it ever disappear automatically? – SpoocyCrep Jul 26 '18 at 07:43
  • The concept of the game is to move your player with your mouse and let enemies get killed if they touch your varying tail.Thanks! – Bak Stak Jul 26 '18 at 08:00
  • I understand, but more importantly is the trail, is it always on the screen or does it disappear after a while? – SpoocyCrep Jul 26 '18 at 08:05
  • The trail appears when player moves and when the player stops the trail gets smaller and smaller till it reaches the player and disappears but when the player moves again the same process is done. – Bak Stak Jul 26 '18 at 08:08
  • Then you should attempt my solution. Its fine if the trail doesn't have a specific length- you should find the right amount of colliders that will work with any length. You can start with a pool size of 400, debug the amount visible at any second and see how much you actually use at the same time and change the number for better performance. You need to understand that the player has a fixed speed and the trail disappears after some time so there IS a maximum length the trail will have at a given time, that maximum length should be equal to the pool size. – SpoocyCrep Jul 26 '18 at 08:10
  • you can always go for a solution like Youssof Hammoud proposed, creating colliders and destroying as you go, but that will be heavier on performance and garbage collector as you will destroy and create new objects as you go. Using pooling gives you more control as you create the colliders in advance. – SpoocyCrep Jul 26 '18 at 08:12
  • Please Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176781/discussion-between-bak-stak-and-spoocycrep). – Bak Stak Jul 26 '18 at 08:12
1

Here is another trick make a script that creates circle colliders (Maybee not the perfect, but a well solution);

The script will instantiate circle collider gameObject that sticks to its initial instantiated position(doesn't change position), enlarge and shrink the circle collider gameObject according to the TrailRenderer width graph in the inspector, and the circle collider will destroy it self when its lifetime is equal to the TrailRenderer's lifetime.

Joe
  • 879
  • 3
  • 14
  • 37
  • "Here is ..." where ? I can't see any link – canbax Aug 25 '19 at 09:29
  • @canbax What is meant is the idea of spawning circle colliders that have static positions as the position of the player. Those colliders will decrease their size over time. Is this clear? There are no referenced links it is just the idea. Thanks. – Joe Aug 27 '19 at 11:06
  • my bad ;) enlarging or shrinking does not really help I guess. Trail might be an arbitrary parabola – canbax Aug 27 '19 at 13:33