Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Elred · Dec 07, 2013 at 08:34 PM · particle animation

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:

alt text

(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?

bug.png (200.9 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aziz1989 · Jan 08, 2016 at 11:51 AM 0
Share

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]);

     }
 }

}

avatar image brunocoimbra Aziz1989 · Jan 08, 2016 at 04:22 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Elred · Dec 07, 2013 at 09:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges