- Home /
How to use sprite mask with trail renderer?
I have two objects. One with a sprite renderer and material, one with a trail renderer. I want the trail to only be visible when it is inside the sprite renderer bounds. Something like sprite mask, but from what I heard, sprite mask doesn't work with trail renderer.
From what I see, that is the opposite of what I want. I want the trail to work with a sprite mask on another sprite renderer, not add a sprite mask on the trail renderer.
Hmm, is your object of a very particular shape, if not you could do it using disctance
Answer by Long2904 · Jul 28, 2021 at 10:17 AM
In the end, I use a second camera with a render texture to achieve this. First, create a camera and a raw image with the same size as my material, then output the camera view to the raw image through a render texture (I do this through code at runtime). Also, remember to set the culling mask of the camera to the trail renderer's layer.
Some example code:
private void Start()
{
renderTex = new RenderTexture(size.x, size.y, 0);
renderTex.Create();
camera = new GameObject("Window Camera", typeof(Camera)).GetComponent<Camera>(); // I create a new game object with a camera rather than add it to the current object because I need to set the camera's pos to (0, 0, -10).
camera.transform.parent = transform;
camera.transform.localPosition = new Vector3(0, 0, -10); // Offset the z coord so that the camera can see stuff
// Set the camera to be 2d
camera.orthographic = true;
camera.orthographicSize = transform.localScale.x / 2;
camera.cullingMask = LayerMask.GetMask("Your Layer Here");
camera.forceIntoRenderTexture = true; // I don't really know if I need this line, but didn't test it yet.
camera.targetTexture = renderTex;
image = GetComponentInChildren<RawImage>();
image.texture = renderTex;
image.canvas.worldCamera = camera;
}
Your answer
Follow this Question
Related Questions
Trail Renderer with visible gaps/breaks in the trail 1 Answer
How can I increase the view-distance of a trail renderer? 1 Answer
Trail Renderer leaving streaks from last position 1 Answer
Trail Renderer 1 Answer
Sprite Mask Render Issue 1 Answer