Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

aligning a particle system

Discussion in 'Physics' started by Ivamaky, Oct 28, 2016.

  1. Ivamaky

    Ivamaky

    Joined:
    Aug 26, 2014
    Posts:
    26
    Hey guys, iam working on something involving tanks and shooting. I intend to spawn a spark particle system (particles emit to Z) when a bullet hits a surface. I want to align the Z axis of the newly spawned particle system to be

    a) in a 90 degree angle to surface that has been hit
    b) in a reflective angle from the original trajectory

    I know that i can "fake it" by setting particle Z to World 90 degrees, however then it will do so on any angled surface, while i would like to have the particle system dynamically orient itself in a 90 degree angle away from the surface that has been hit/ Or reflect into the direction away from the incoming bullet.

    hope you can help me, getting quite frustrated.
    i added a quick drawing of my intent

    i cant code and am using visual scripting playmaker/flowcanvas, what iam after is understanding the process of what operations i need to do to get to my result, iam just completely lost how to figure out to obtain the angles/vectors required
     

    Attached Files:

    • expl.png
      expl.png
      File size:
      30.9 KB
      Views:
      935
  2. Ivamaky

    Ivamaky

    Joined:
    Aug 26, 2014
    Posts:
    26
  3. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You can't do both of these, since an axis only points in one direction. Or maybe you just want to choose one at random?

    A vector that is orthogonal (at 90 degrees) to a surface is called a 'normal'. When you use OnCollisionEnter or OnCollisionEnter2D, you get a Collision or Collision2D back. This has a member called contacts, which is of type ContactPoint[] or ContactPoint2D[] and is an array of all the points of contact between the colliders in the collision. Each contact has a normal member that you can read to see what the normal of the surface is. Here's an example:

    Code (CSharp):
    1. void OnCollisionEnter(Collision other)
    2. {
    3.     Vector3 normalVector;
    4.  
    5.     normalVector = other.contacts[0].normal;    //The normal vector for the fist contact point is stored in 'normalVector'
    6.     //...
    7. }
    You can align the z-axis of the particle object to this vector with Transform.forward:

    Code (CSharp):
    1. particles.transform.forward = normalVector;
    If you want the reflection vector instead, use Vector3.Reflect() with the bullet's forward vector:

    Code (CSharp):
    1. //...
    2.  
    3. Vector3 reflectVector = Vector3.Reflect(bullet.transform.forward, normalVector);    //Assumes that the bullet travels along its local z axis; use transform.up or transform.right instead if needed
    4. particles.transform.forward = reflectVector;
    5.  
    6. //...
    There is often more than one contact point. A common procedure is to take the mean of normal vectors of each contact, but I've found that for simple objects, just using the first one (other.contacts[0]) is sufficient. For complex objects with lots of points that stick out, taking the mean might be illogical.
     
    Last edited: Nov 12, 2016
  4. Ivamaky

    Ivamaky

    Joined:
    Aug 26, 2014
    Posts:
    26
    aligning transform forward to vector normal sounds like the solution, ill try that one first and then report back. Thank you very much so far! And yes i ment either do one or the other! :)
     
  5. Ivamaky

    Ivamaky

    Joined:
    Aug 26, 2014
    Posts:
    26
    That was exactly it! by setting the Particle system forward to be the collision normal i got the a) working, ill try your suggestion for b) as well

    thank you so much already tho!