Posted By

admanic on 01/31/18


Tagged

Unity trail


Versions (?)

Unity Trail


 / Published in: C#
 

URL: unityTrail

a quick script to add a trail to your transforms.

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GizmoTrail : MonoBehaviour {
  6.  
  7. //Variables
  8. public int MaxTrailSize = 1000;
  9. public float SphereRadius = 0.5f;
  10. public Color TrailColor;
  11. public float TrailTickTime = 1f;
  12.  
  13. float tick;
  14. List<Vector3> positions;
  15.  
  16. // Use this for initialization
  17. void Start () {
  18. positions = new List<Vector3>();
  19. }
  20.  
  21. // Update is called once per frame
  22. void FixedUpdate () {
  23. tick += Time.fixedDeltaTime;
  24. if (tick >= TrailTickTime)
  25. {
  26. if (positions.Count > MaxTrailSize)
  27. {
  28. positions.RemoveAt(0);
  29. }
  30. positions.Add(transform.position);
  31. tick = 0f;
  32. }
  33. }
  34.  
  35. private void OnDrawGizmos()
  36. {
  37. if (!Application.isPlaying)
  38. return;
  39.  
  40. Gizmos.color = TrailColor;
  41. for(int i = 0; i < positions.Count; i++)
  42. {
  43. Gizmos.DrawSphere(positions[i], SphereRadius);
  44. if(i != 0)
  45. {
  46. Gizmos.DrawLine(positions[i], positions[i - 1]);
  47. }
  48. }
  49. }
  50. }

Report this snippet  

You need to login to post a comment.