I have a game object that doesn’t move vertically in 2D and I want the trail renderer underneath it as if it is constantly moving vertically, but because the sprite moves horizontally, the trail renderer appears horizontally. How can i change this trailer renderer as if it is moving vertically ?
I’m trying hard to picture what you want but I think the closest thing would be a LineRenderer where you can set the points in whatever direction you like.
Attach the trail renderer to a separate object whose position you are updating “manually” in an Update loop (with x and y inverted for example).
think like that: There is a 2D spaceship game, the spaceship is fixed in the y coordinate and goes left and right on the x coordinate. The background is constantly shifting down the y-axis to make it look like the spaceship is moving. I’m trying to add a trail renderer behind the spaceship so that it looks like it’s going forward, leaving a trail behind it as if it’s going forward, but since the spaceship is stationary in the y-coordinate and moving in the x-coordinate, the trail renderer leaves a trace in the x-coordinate. I want this trace to be in y coordinate
how can i add a continuous line to a stationary object
LineRenderer will be your go-to.
You’ll need an array of points that you move along downwards, removing the bottom one and adding to the top one based on your ship’s current position.
Here was my quick take on it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker
// to use:
// - put this on your left/right spaceship at a transform where you want the line to start
// - make a LineRenderer somewhere else, drag it into the public field here
// - set the count of points
// - set the downward speed
public class StreakingDown : MonoBehaviour
{
[Header( "The line streak (make it, drag it in)")]
[Header( "Be sure to use world coordinates.")]
public LineRenderer LRStreak;
[Header( "Determines how many frames we go back.")]
public int VertexCount = 30;
[Header( "Units per second.")]
public float VerticalSpeed = -10;
Vector3[] historicalPoints;
void Update ()
{
// sample position
Vector3 here = transform.position;
// init / reinit
if (historicalPoints == null || historicalPoints.Length != VertexCount)
{
historicalPoints = new Vector3[ VertexCount];
for (int i = 0; i < historicalPoints.Length; i++)
{
historicalPoints[i] = here;
}
}
// age 'em
for (int i = historicalPoints.Length - 2; i >= 0; i--)
{
Vector3 position = historicalPoints[i];
position += Vector3.up * VerticalSpeed * Time.deltaTime;
historicalPoints[i + 1] = position;
}
historicalPoints[0] = here;
// drive them to the LR
LRStreak.positionCount = historicalPoints.Length;
LRStreak.SetPositions( historicalPoints);
}
}
U r the best ! Thanks for helping
Or a particle system
That’s actually a WAY better way!!! Nice idea sngdan… that didn’t even occur to me. No code required!
OP: if you go with particle system, be sure to select world coordinates or the particles will stay with your ship and look wrong.