Fix particleSystem speed ?

Updated on December 12, 2016 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on December 10, 2016

When I kill my enemy some kind of gold get out of him. This gold moves forward to the player if he gets close. It moves very slow. It becomes more faster when get close to the player.


using UnityEngine;

using System.Collections;
public class souls : MonoBehaviour {
[SerializeField]

public Transform _attractorTransform;

private float paticlesFarFrom;

private ParticleSystem _particleSystem;

private ParticleSystem.Particle[] _particles = new ParticleSystem.Particle[1000];
void Start ()

{

_particleSystem = GetComponent<ParticleSystem> ();

}
public void Update()

{

if (GetComponent<ParticleSystem>().isPlaying) {

int length = _particleSystem.GetParticles (_particles);

Vector3 attractorPosition = _attractorTransform.position;

for (int i=0; i < length; i++) {

paticlesFarFrom = Vector3.Distance(_particles[i].position,_attractorTransform.transform.position);

if (paticlesFarFrom <=200) {

_particles [i].position = _particles [i].position + (attractorPosition - _particles [i].position) / (_particles [i].remainingLifetime) * Time.deltaTime;
}

}

_particleSystem.SetParticles (_particles, length);

}
}

}

  • Liked by
Reply
5 on December 10, 2016

What’s the question?

on December 11, 2016
What’s the question?From Dion Dokter
if (paticlesFarFrom <=200) {
 
_particles [i].position = _particles [i].position + (attractorPosition - _particles [i].position) / (_particles [i].remainingLifetime) * Time.deltaTime;
}

No matter what i do it moves (particle system) very slow. How to increase the speed ?

Devoted
on December 11, 2016

So, In the world of physics (our world), things that are usually attracted to each other, do so on a R^2 base system. For example, two oppositely charged particles, q1 and q2 are atteracted to each other by a force of F = k*q1*q2/(r^2) where k is just a known constant. 

I’d suggest applying this idea of r^2 similarly, so maybe something like this…

_particles [i].position = _particles [i].position + (attractorPosition - _particles [i].position) / (_particles [i].remainingLifetime) * Time.deltaTime;
Vector3 deltaPos = variableConstant / ((attractorPosition - _particles[i].position)*(attractorPosition - _particles [i].position) ) ; // It will take some trial and error to get the variableConstant to the correct float, but you can do it!
_particles [i].position = _particles [i].position + deltaPos;

 
I feel like this will at least get you on a good track for developing a more natural process that you’re looking for.

on December 11, 2016

Vector3 deltaPos = 2 / ((attractorPosition - _particles[i].position)*(attractorPosition - _particles [i].position) ) ;
//---------
 Error CS0019: Operator '*' cannot be applied to operands of type 'UnityEngine.Vector3' and 'UnityEngine.Vector3' 

thank you for your reply. I got this error and i don’t know how to fix it.

Devoted
on December 11, 2016

Ah, right. I forgot about the fact that you can’t multiply ( * ) vectors in Unity, so, here’s what we do instead…
 
We square the distance between the two positions,
then we multiply that by the direction between those two positions.
Like so…

Vector3 deltaPos = 2 / ( Vector3.SqrMagnitude (attractorPosition - _particles [i].position) * Vector3.Normalize( attractorPosition - _particles [i].position ) ); // don't forget to try a whole bunch of values for '2' at the beginning there. I've no idea what might look good. Maybe 0.2, maybe 200. It will take some tweaking.

Let me know how that works for you.

on December 12, 2016

same error. this time with the number “2” whether it was int or float !!
this what i do
Example – fable 2

Show more replies
  • Liked by
Reply
Cancel
1 on December 10, 2016

Perhaps it would be easier to instantiate an object, check the range to the player and transform it towards him?

on December 11, 2016

i did this before and the result was awful.

Show more replies
  • Liked by
Reply
Cancel