Search Unity

Bent trails with artifacts

Discussion in 'General Graphics' started by Redden44, Jan 9, 2018.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    136
    Hello, I'm having problems with trails, they are bent and sometimes show weird artifacts too:







    I'm using Unity 2017.2.1p1 and I can't upgrade to the newest version because it bugs my UI.
    Is this a bug or is it an error in my code? I don't think I've ever noticed this bug in older versions of Unity, but I might be mistaken.

    This is the code of the skill used in the screenshot:
    Code (CSharp):
    1. public void Skill2A()
    2.     {
    3.         float projNum = 5;
    4.         float multishotAngle = 6f;
    5.         Vector2 projPos = new Vector2(transform.position.x, transform.position.y + (transform.Find("Hitbox").GetComponent<BoxCollider2D>().size.y / 2));
    6.         Vector2 targetPos = _player.skillPos;
    7.         Vector2 projDir = (targetPos - projPos).normalized;
    8.         float dirAngle = Vector2.Angle(Vector2.right, projDir);
    9.         float distY = (projPos.y - targetPos.y);
    10.         if (distY > 0)
    11.             dirAngle = 360 - dirAngle;
    12.         PlayerDmgInfo dmgInfo = _statsManager.CalculateDamageToGive(_skill2ADmg, PlayerDmgInfo.DmgType.physical, 2);
    13.         dirAngle = dirAngle - ((projNum - 1) / 2 * multishotAngle);
    14.         for (int i = 0; i < projNum; i++)
    15.         {
    16.             bool canPierce = false;
    17.             if (Random.Range(1, 100 + 1) <= piercingAtkChance)
    18.                 canPierce = true;
    19.             Vector2 newProjDir = (Quaternion.AngleAxis(dirAngle + multishotAngle * i, Vector3.forward) * Vector3.right).normalized;
    20.             Vector2 newProjPos = projPos + (newProjDir * 0.5f);
    21.             GameObject newProj = Instantiate(_multishotArrow, newProjPos, Quaternion.identity);
    22.             newProj.transform.rotation = Quaternion.Euler(0, 0, dirAngle + multishotAngle * i);
    23.             newProj.GetComponent<RangerMultishotArrow>().InitializeProjectile(newProjDir, dmgInfo, ccInfo, canPierce, _statsManager);
    24.             newProj.GetComponent<RangerMultishotArrow>().InitializeAudio(_audioManager, _multishotHit);
    25.         }
    26.     }
    Basically I spawn an arrow, than rotate it and initialize it by giving it the direction it needs to follow. Then I move the arrow with this "transform.Translate(_direction * _speed * Time.fixedDeltaTime, Space.World);" inside the FixedUpdate() function.

    Thanks for your help.
     
  2. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    136
    Anyone please?
     
  3. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    286
    Have you tried calling Clear() on TrailRenderers after spawning them?
     
  4. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    136
    That worked! Thank you very much!

    Is that a normal thing to do? I don't think it was necessary before, I wonder why it is now.
     
  5. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    286
    I don't actually know if it's a bug. I noticed similar thing when upgrading from 5.6 to 2017.1 on our project; trails were drawing from the origin to the point where they were instantiated at. Perhaps it's an optimization (after all it's simple to call Clear() if needed), or just a plain bug.
     
    Redden44 likes this.
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,188
    There has been a bit of debate about how this should work in recent Unity versions. Both behaviors can be seen as correct, depending on your use case.

    Some users want Trails to drop a point where they are spawned, and some users want to be able to spawn+teleport, and for this initial teleportation not to drop a trail (to allow for custom initial placement).

    For the users that expect a trail to start exactly from the spawn position, we made the behavior change you have noticed.

    The reason for this is, it's relatively simple to call Clear, as you have done, to get back your old behavior, whereas, for the opposite case, there is no simple workaround. So hopefully this change allows everyone to set their trails up the way they want :) (it's just annoying that we had to make a change to existing behavior, for this, which we never like to do)