简体   繁体   中英

Trail renderer is leaving a trail when teleporting

i have a game like fruit ninja where a blade follows your finger and a child trail renderer will follow.

This works fine in the editor but when i built the apk and played on my phone, it teleports from its last point. So if i move in the top left corner, lift up my finger and place it on the bottom right, u will see a thin quick diagonal line.

Here is my code:

private void Update()
{
    if (Event.current == null || (Event.current != null && EventSystem.current.currentSelectedGameObject == null))
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            circleCollider.enabled = false;
            currentTrail = Instantiate(trail, transform);
            isCutting = true;
        }
        else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            circleCollider.enabled = false;
            currentTrail.transform.SetParent(null);
            Destroy(currentTrail, 2f);
            isCutting = false;
        }
    }

if (isCutting)
    {
        Vector2 newPos = currentTrail.transform.position = rb.position = cam.ScreenToWorldPoint(Input.mousePosition);

        float velocity = (newPos - previousPos).magnitude * Time.deltaTime;
        if (velocity > minCuttingVelocity)
        {
            circleCollider.enabled = true;
            canCut = true;
        } else {
            circleCollider.enabled = false;
            canCut = false;
        }

        previousPos = newPos;
    }
}

As i said this works fine in the editor, its just on phone. Is there any hacky way i can do to get around this?

When you think about it, in unity, any movement is just a small teleport each frame. By this logic the trail renderer must be based on such movement. How can the trail renderer tell the difference between a big teleport and a little one? It probably can't.

So if you were going to explicitly teleport an object, disable its trail render, teleport it, and then re-enable it. should do the trick.

you can stop the trail with TrailRenderer.emitting = false befor teleporting and set it to true after.

just check if the difference between the new and old position the do:

if(distance > notmalMovingDistance)
    myTrail.emitting = false;
else
    myTrail.emitting = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM