Trail effect for non-moving object
Hello. I'm making a vertical SHMUP type of game (similar to Skyforce). Player is not moving, level does.
Because of that I can't use trail renderer to create a trail effect for my spaceship's engines.
This is how I want it to look in game: http://i.imgur.com/szUKxTM.png
Particle system looks wierd: http://i.imgur.com/E8nyvoz.png
Trail renderer won't work when you're not moving...
Any ideas how to make trail look like line and also follow spaceship's movement?
I've heard that LineRenderer can simulate a TrailRenderer. Is this true? How? If not, what can I do?
Answer by HowlinnWolf · Apr 21, 2017 at 08:39 PM
Allright. I got a solution from a member of another community.
He suggested to:
Use a line renderer to simulate a trail renderer. Points on the line renderer move downwards over time, and new ones are added at the ship.
It looks like this: https://gyazo.com/7f4bf7a45c4797729919ff7c39b33547
This is how it looks in my project: http://i.imgur.com/cpYsN0b.gifv
Here's the code they gave to me:
public float lifetime = 5f; //lifetime of a point on the trail
public float minimumVertexDistance = 0.1f; //minimum distance moved before a new point is solidified.
public Vector3 velocity; //direction the points are moving
LineRenderer line;
//position data
List<Vector3> points;
Queue<float> spawnTimes = new Queue<float>(); //list of spawn times, to simulate lifetime. Back of the queue is vertex 1, front of the queue is the end of the trail.
//Length of this queue is one less than the number of positions in the linerenderer, since the linerenderer always has a non-solidified vertex at the object's position.
// Use this for initialization
void Awake () {
line = GetComponent<LineRenderer>();
line.useWorldSpace = true;
points = new List<Vector3>() { transform.position }; //indices 1 - end are solidified points, index 0 is always transform.position
line.SetPositions(points.ToArray());
}
void AddPoint(Vector3 position) {
points.Insert(1, position);
spawnTimes.Enqueue(Time.time);
}
void RemovePoint() {
spawnTimes.Dequeue();
points.RemoveAt(points.Count - 1); //remove corresponding oldest point at the end
}
// Update is called once per frame
void Update () {
//cull based on lifetime
while(spawnTimes.Count > 0 && spawnTimes.Peek() + lifetime < Time.time) {
RemovePoint();
}
//move positions
Vector3 diff = -velocity * Time.deltaTime;
for(int i = 1; i < points.Count; i++) {
points[i] += diff;
}
//add new point
if (points.Count < 2 || Vector3.Distance(transform.position, points[1]) > minimumVertexDistance) {
//if we have no solidified points, or we've moved enough for a new point
AddPoint(transform.position);
}
//update index 0;
points[0] = transform.position;
//save result
line.positionCount = points.Count;
line.SetPositions(points.ToArray());
}
}
Hey i need help pleaze i'm new in unity i perfectly added this script and now my trail is green and looks ugly i want to replace it by a trail i have drawed in adobe illustrator is this possible ?
Your answer
Follow this Question
Related Questions
[Solved] I am trying to build a space sim/ shooter type game. 1 Answer
Project Space Shooter: My ship won't move! 2 Answers
Keeping player within screenbounds with moving camera (Vertical Shooter) 2 Answers
HLAPI simple 2 player game 1 Answer
He wants to write a script on a page change gait from right to left or vice versa 0 Answers