- Home /
Trying to animate particles via script, troubling result
Hello,
I'm messing around with particles, and I've bumped into a result I don't understand.
What I'm trying to do is making all particles emitted by a specific gameobject drift/move toward another gameobject. My target point is a public variable I set up manually, and I'm using the Lerp vector3 method to change the position of my particles.
This is the code of my whole class:
using UnityEngine;
using System.Collections;
public class goToward : MonoBehaviour {
public GameObject m_Destination;
private ParticleSystem.Particle[] m_Particles= new ParticleSystem.Particle[5000];
void Start () {
}
void Update () {
int particleLength = particleSystem.GetParticles (m_Particles);;
for (int i=0; i<particleLength;i++) {
m_Particles[i].position=Vector3.Lerp(m_Particles[i].position,m_Destination.transform.position,0.1f);
}
particleSystem.SetParticles (m_Particles,particleLength);
}
}
So, what I have trouble understanding is this: with this code, all particles emitted by the gameobject actually stop halfway from the target gameobject. Here is a skillfully edited screenshot showcasing my problem:
(edit: well derp, text is too small to be read, but the attached image is the correct size!)
Is there some subtelty with either Lerp or ParticleSystem I'm missing that create this result?
HI everyone ...
i am doind basecally the same thing only ins$$anonymous$$d of destination gameobjects in your project i am trying to catch the initial position of every particle and after a hit with a collider it supposed to get back to this position. i catched all this in arrays (particles and every position) but when i "Debug.Log there positions" it is always (0.0,0.0,0.0) and it usually crashes the editor... there is my code if you want to help me. thx
using UnityEngine; using System.Collections;
public class ParticleBehavior : $$anonymous$$onoBehaviour {
private ParticleSystem m_System;
private ParticleSystem.Particle[] m_Particles;
private Vector3[] particleInitialPos;
//[Range(0f,12f)] // public float lifetime; // [Range(0, 2000)] // public int particleNumber;
void Awake()
{
m_System = GetComponent<ParticleSystem>();
m_Particles = new ParticleSystem.Particle[600];
particleInitialPos = new Vector3[600];
for (int i = 0; i < particleInitialPos.Length; i++)
{
particleInitialPos[i] = m_Particles[i].position;
new GameObject("Pos"+i);
//Debug.Log(i + " : " + m_Particles[i].position);
//Debug.Log(i + " : " + particleInitialPos[i]);
}
//m_Particles = GetComponents<ParticleSystem.Particle>();
}
// Update is called once per frame
void Update()
{
int numParticlesAlive = m_System.GetParticles(m_Particles);
if (numParticlesAlive >= m_Particles.Length || !WindCreator.fingerIsOff)
{
m_System.enableEmission = false;
}
if (WindCreator.fingerIsOff ) // if the player's finger is up the counting will start;
{
m_System.SetParticles(m_Particles, m_System.GetParticles(m_Particles));
Invoke("SmokeClosure",3);
}
for (int i = 0; i < m_Particles.Length; i++)
{
if (m_Particles[i].lifetime <= 1)
{
m_Particles[i].lifetime = m_Particles[i].startLifetime;
}
}
m_System.SetParticles(m_Particles, numParticlesAlive);
}
void OnParticleCollision(GameObject other)
{
if (other.gameObject.tag == "Smoke$$anonymous$$iller")
{
this.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
}
}
void SmokeClosure() // the function that allows particle to gather again
{
for (int i = 0; i < m_Particles.Length; i++)
{
m_Particles[i].position = Vector3.Lerp(m_Particles[i].position, particleInitialPos[i], 0.5f);
//Debug.Log(i+" : "+m_Particles[i].position);
//Debug.Log(i + " : " + particleInitialPos[i]);
}
}
}
If you want someone to answer you, open a new question topic, it's a bad practice to post unecessary comments on already answered questions.
Answer by GameVortex · Dec 07, 2013 at 08:53 PM
Your simulation space is Local, so the particles position is in the local coordinate system.
Depending on how you want your system to behave, either change the particle system to simulate in World space, or transform the position of that target into local space with InverseTransformPoint like this:
m_Particles[i].position = Vector3.Lerp(m_Particles[i].position, transform.InverseTransformPoint(m_Destination.transform.position), 0.1f);
You are my hero. Setting simulation space to world indeed worked. I'm more used to manipulating global coordinates in other projects, it didn't even cross my $$anonymous$$d it could be that (I know it can be more convenient to work locally in some case). Thanks a lot!
Your answer
Follow this Question
Related Questions
Particle orientation from parent 0 Answers
Help Learning Photon Networking with C# 1 Answer
Particle System (shuriken) with mesh particles, change color? 0 Answers
Not understanding [RPC] 0 Answers
Weapon attatchments system? 2 Answers