Skip to content

Instantly share code, notes, and snippets.

@codorizzi
Last active June 1, 2021 07:44
Show Gist options
  • Save codorizzi/c1e2dac50154dd20d874f4d58ace62ac to your computer and use it in GitHub Desktop.
Save codorizzi/c1e2dac50154dd20d874f4d58ace62ac to your computer and use it in GitHub Desktop.
Creates a line between two points using a particle system
using Sirenix.OdinInspector;
using UnityEngine;
namespace GameAssets.Scripts.Utility {
public class ParticleSystemLine : MonoBehaviour {
#region Target
[FoldoutGroup("Target")]
[OnValueChanged("UpdateTarget")] public bool useTransform = true;
[ShowIf("useTransform")]
[FoldoutGroup("Target")]
[OnValueChanged("UpdateTarget")] public Transform targetTransform;
[FoldoutGroup("Target")]
[HideIf("useTransform"), ShowInInspector, OnValueChanged("UpdateTarget")] public Vector3 TargetPosition {
get => useTransform ? targetTransform != null ? targetTransform.position : Vector3.zero : _targetPosition;
set => _targetPosition = value;
}
#endregion
#region Private Fields
private Vector3 _targetPosition;
private ParticleSystem.MainModule _mainModule;
private ParticleSystem.EmissionModule _emission;
private ParticleSystem _particleSystem;
private ParticleSystemRenderer _renderer;
private float Distance => Vector3.Distance(transform.position, TargetPosition);
#endregion
#region Settings
[FoldoutGroup("Settings")] [HideInPlayMode] public bool pauseOnStart = false;
[FoldoutGroup("Settings")] [OnValueChanged("UpdateTarget")] public float speed = 2f;
[FoldoutGroup("Settings")] [OnValueChanged("UpdateTarget")] public Material material;
[FoldoutGroup("Settings")] [OnValueChanged("UpdateTarget")] public float emissionRate = 1f;
#endregion
[ShowInInspector, HideInEditorMode, PropertySpace(5, 5)] public bool Play {
get => _particleSystem != null && _particleSystem.isPlaying;
set {
if (value) {
_particleSystem.Stop();
_particleSystem.Play();
} else {
_particleSystem.Pause();
}
}
}
void Awake() {
_particleSystem = GetComponent<ParticleSystem>();
_renderer = GetComponent<ParticleSystemRenderer>();
_mainModule = _particleSystem.main;
_emission = _particleSystem.emission;
}
private void Start() {
if(pauseOnStart)
_particleSystem.Pause();
UpdateTarget();
}
void UpdateTarget() {
if(Application.isEditor) {
_particleSystem = GetComponent<ParticleSystem>();
_mainModule = GetComponent<ParticleSystem>().main;
_emission = GetComponent<ParticleSystem>().emission;
_renderer = GetComponent<ParticleSystemRenderer>();
}
SetMain();
SetRenderer();
SetEmission();
_particleSystem.Stop();
_particleSystem.Play();
}
void SetRenderer() {
_renderer.renderMode = ParticleSystemRenderMode.Stretch;
_renderer.cameraVelocityScale = 0;
_renderer.velocityScale = 0;
_renderer.lengthScale = 1;
_renderer.freeformStretching = true;
_renderer.rotateWithStretchDirection = true;
if (material != null)
_renderer.material = material;
}
void SetMain() {
_mainModule.startSpeed = speed;
_mainModule.startLifetime = Distance / speed;
_mainModule.maxParticles = 999;
transform.LookAt(TargetPosition);
}
void SetEmission() {
_emission.rateOverTime = emissionRate;
}
}
}
@codorizzi
Copy link
Author

Odin code will need to be removed if not used in project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment