- Home /
How to make particles not disappear when called a second time
I'm trying to make a nice particle system that stays for a while, but the thing is, the particles disappear instantly once I call the particles again. How would I make the particles not disappear upon calling it again while not having to deal with prefabs and instantiation? This is my code script for moving the character:
using System.ComponentModel;
using System.Timers;
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ParticleSystemJobs;
public class Move : MonoBehaviour
{
[SerializeField]
private float speed;
[SerializeField]
private float dashSpeed;
[SerializeField]
private float dashInterval;
[SerializeField]
private int dashLength;
[SerializeField]
private bool jumps;
[SerializeField]
private bool dashes;
[SerializeField]
private bool maxDashes;
[SerializeField]
private int maxJumps;
[SerializeField]
private float jumpHeight;
[SerializeField]
private bool dashing = false;
//[SerializeField]
//private float jumpHeight1;
//[SerializeField]
//private float jumpHeight2;
private Rigidbody2D rb2D;
public GameOver gameOver;
public ParticleSystem dashPart;
public ParticleSystem landPart;
public ParticleSystem deathPart;
public Animator right;
private Vector3 curPos;
private Vector3 lastPos;
public Sprite artDeath;
public Vector3 rotation;
private void Start()
{
dashPart.Stop();
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
transform.eulerAngles = rotation;
if(dashing == false)
{
if(Input.GetKey("d"))
{
rotation = new Vector3(0f, 0f, 0f);
transform.Translate(speed * Time.deltaTime, 0f, 0f);
}
if(Input.GetKey("a"))
{
rotation = new Vector3(0f, 180f, 0f);
transform.Translate(speed * Time.deltaTime, 0f, 0f);
}
if(Input.GetKeyDown("space"))
{
if(jumps == true)
{
StartCoroutine(Jump());
}
}
}
curPos = transform.position;
if(curPos == lastPos)
{
right.SetFloat("Move", -1f);
}
else
{
right.SetFloat("Move", 1f);
}
lastPos = curPos;
if(Input.GetKeyDown("left shift"))
{
StartCoroutine(Dash());
}
}
void OnTriggerStay2D(Collider2D other)
{
dashes = true;
if(other.gameObject.CompareTag("Enemy"))
{
gameObject.GetComponent<Move>().enabled = false;
right.SetFloat("Death", 1f);
gameOver.GameEnd();
}
}
void OnTriggerEnter2D(Collider2D other)
{
float vel = rb2D.velocity.y;
int emitInt = Convert.ToInt32(vel);
//Debug.Log(emitInt.ToString());
if(other.gameObject.CompareTag("Ground"))
{
jumps = true;
dashes = true;
right.SetFloat("Jump", 0f);
landPart.gameObject.GetComponent<PartMoove>().Move();
landPart.emission.SetBursts(
new ParticleSystem.Burst[]{
new ParticleSystem.Burst(0f, -emitInt * 2)
});
landPart.Play();
}
if(other.gameObject.CompareTag("Enemy"))
{
gameObject.GetComponent<Move>().enabled = false;
right.SetFloat("Death", 1f);
gameOver.GameEnd();
}
}
private void OnTriggerExit2D(Collider2D col)
{
if(col.gameObject.CompareTag("Ground"))
{
jumps = false;
}
}
IEnumerator Jump()
{
jumps = false;
right.SetFloat("Jump", 1f);
yield return new WaitForSeconds(0.2f);
rb2D.AddForce(transform.up * jumpHeight);
//transform.Translate(Vector3.up * jumpHeight * Time.deltaTime);
//transform.position += Vector3.up * jumpHeight * Time.deltaTime;
}
IEnumerator Dash()
{
if(dashes == true)
{
dashPart.Play();
dashes = false;
for(int i = dashLength; i > -1; i--)
{
if(i <= 0)
{
dashPart.gameObject.GetComponent<PartMoove>().Move();
right.SetFloat("Dash", 0f);
rb2D.gravityScale = 1.3f;
dashing = false;
dashPart.Stop();
rb2D.constraints = RigidbodyConstraints2D.None;
}
else
{
dashPart.gameObject.GetComponent<PartMoove>().Move();
right.SetFloat("Dash", 1f);
dashing = true;
rb2D.gravityScale = .1f;
rb2D.constraints = RigidbodyConstraints2D.FreezePositionY;
}
yield return new WaitForSeconds(dashInterval);
transform.Translate(dashSpeed * Time.deltaTime, 0f, 0f);
}
}
}
}
script for moving particles to the player but not stick to it: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PartMoove : MonoBehaviour
{
public Transform targetpart;
public bool rotato;
public float offsety;
public void Move()
{
transform.position = new Vector3(targetpart.position.x,targetpart.position.y-offsety,targetpart.position.z);
if(rotato == true)
{
if(targetpart.eulerAngles.y == 0f)
{
transform.eulerAngles = new Vector3(0f,-90f,0f);
}
else if(targetpart.eulerAngles.y == 180)
{
transform.eulerAngles = new Vector3(0f,-270f,0f);
}
}
}
}
Also, as a side note, does anyone know how to add friction to the particle collisions?
Your answer
Follow this Question
Related Questions
How can I create a particle 'vortex' or implosion using Shuriken? 2 Answers
Can I slow down a Particle System? - e.g. Slow Motion Effect 1 Answer
How to Slowly Fade Particle System via Script 2 Answers
Particle sorting/layering question 0 Answers
Spawned Particle System in game looks different from prefab 2 Answers