Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dark_Seth · Sep 21, 2015 at 02:34 PM · scripting problemparticlesystem

Particle Simulate Local And World Space question

Guys

I need your help. The following script kills the particle when it get to the target. When I set my particle System to Emulate Local Space it can't calculate the particle position in World Space but my energy beam behaves like it should ( Direction wise but goes past the target). If I set the particles to World Space. It dies at the target cause the distance gets calculated in word space which is correct. What I want to do is simulate in Local Space ( Energy Beam looks behaves nice) But use the particles Word position to calculate the distance. Please see Video to clarify what I am trying to say.

https://www.youtube.com/watch?v=--QvAGjO8bw

 var laser : ParticleSystem ;
 var particles : ParticleSystem.Particle[];
 var target : Transform;
 var endpoint : Vector3;
  
  
 function Update(){
  
 }
  
 function LateUpdate() {
  
 InitializeIfNeeded();
   var lenght = laser.GetParticles(particles);
   transform.LookAt(target);
   endpoint = target.position;
  
   for (var i = 0; i < lenght; i++) {
  
  
  
   if(Vector3.Distance(target.position, particles[i].position) < 2f) {
   particles[i].lifetime = -0.1f; //Kill the particle
  
  
   }
  
   }


Comment
Add comment
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

1 Reply

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

Answer by NoseKills · Sep 21, 2015 at 08:53 PM

Actually it even says it in the API docs for Particle.position. You can use Transform.InverseTransformPoint() to get the world position in local space :)

 var targetLocal = transform.InverseTransformPoint(target.position);
 ...
 if (Vector3.Distance(targetLocal, particles[i].position) < 2f)

Also, it's a small thing but whenever you only compare distances ( Vector3.Distance(a, b) < x ) and you don't really need to know the actual distance in units, it's usually better to use properties / functions like Vector3.sqrMagnitude that don't do the last step of the Pythagoras formula to calculate the length of the distance vector (square root) which is a bit performance heavy.

 // particle emitter is at(0,0) in this object, so this is also the distance vector to target
 var targetLocal = transform.InverseTransformPoint(target.position);
     
 for (var i = 0; i < lenght; i++) {
     // distance vector between particle and target
     var distToTarget : Vector3 = particles[i].position - targetLocal;
     // compare the square of distance to square of your limit (2f) 
    if (distToTarget.sqrMagnitude < 4f) ...

Or even better, since you need to compare if a particle has gone past a certain point (if it's really fast, it might slip through your current check where it needs to be close to target) you could use dot product. It's a fast calculation that checks whether 2 vectors point in similar direction ( dot > 0 ) or opposite direction ( dot < 0 ).

 // particle emitter is at(0,0) in this object, so this is also the distance vector to target
 var targetLocal = transform.InverseTransformPoint(target.position);
 for (var i = 0; i < lenght; i++) {
         var distToTarget : Vector3 = particles[i].position - targetLocal;
         // if vector from particle source to target points in the same direction as
         // vector from target to particle, it means the particle has passed the target 
         if (Vector3.Dot(distToTarget, targetLocal) > 0) ...


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 Dark_Seth · Sep 21, 2015 at 09:39 PM 0
Share

@nosekills

Thanks. On my way to post here. I got it working using Simulate Local World Space. I did a very similar thing.

 var  localPos : Vector3 = transform.InverseTransformDirection(target.position - transform.position);

and then Compared the distance. This gave me a perfect result.

I tried using your but that gave me dash like effect for some reason.

Then I combined my script above with

 var distToTarget : Vector3 = particles[i].position - localPos;

and

 if (Vector3.Dot(distToTarget, localPos) > 0)

making the calculation faster like you Said.

Thank you!

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating Meteor with Physics (where the Particle System Works) 0 Answers

Script not playing Particle Effect 0 Answers

Play sound on particle death C# 0 Answers

How can I get the game object that a particle trigger detects? 0 Answers

Particle System trigger script not working,Particle system trigger script not working 0 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