Unity 2D Trail Renderer 碰撞 [英] Unity 2D Trail Renderer Collision

查看:31
本文介绍了Unity 2D Trail Renderer 碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作 2D 统一游戏,但我面临着我的游戏所依赖的一个主要问题.

我在我的播放器上附加了一个轨迹渲染器组件,我需要的是让渲染器成为一个碰撞器,充当 MeshCollider 我只是不知道是否可以制作一个碰撞器采用 2D 轨迹渲染器的形状.

我在谷歌上搜索过,但没有一个性能良好的解决方案:

  • 有人说创建一个空的游戏对象并附加跟踪渲染器组件,然后向其添加一个碰撞器.但也不起作用.
  • 我尝试关注

    是否可以编写一些脚本来实现我的目标或者 UnityEngine 有一个渲染解决方案.提前致谢.

    在我复制脚本并运行后,轨迹发生碰撞,但当玩家不移动时它会表现得很愚蠢.

    游戏基本上是一个transform.position等于鼠标位置的玩家.所以轨迹没有特定的长度.

    解决方案

    我的做法如下:

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

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

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

    这篇关于Unity 2D Trail Renderer 碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆