Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Transform.position calculation help

Discussion in 'Scripting' started by ssaass227, Dec 1, 2023.

  1. ssaass227

    ssaass227

    Joined:
    Dec 27, 2020
    Posts:
    17
    Hello devs, do someone know how to calculate proper position.

    a problem is - particles should emit at collision point but it differs according world position of collision point, and i've made wrong position calcs --> you can see it in a video, 2nd half of the video.

    Custom_SimSpace_Transform is "ParticlesSystem's" Custom Simulation Space and it is located at center of car.
    Here is script , thx in advance

    Code (CSharp):
    1. private void OnCollisionEnter(Collision collision)
    2.             {
    3.                 var emitParams = new ParticleSystem.EmitParams();
    4.  
    5.              //   IF I USE
    6.                 emitParams.position = new Vector3(0, 0, 0);
    7.              //   It will emit particles at "Custom_SimSpace_Transform" it is -> for example at (257,533,1043) in world postition . in other words it'll emits at center of car
    8.              //   So i made this thing bellow
    9.                 emitParams.position = Custom_SimSpace_Transform.position - collision.contacts[0].point;       < ---I assume ,Issue is here
    10.              //   But it works not right as shown in video  , 2nd half  of the video.
    11.  
    12.                 emitParams.applyShapeToPosition = true;
    13.                 Emit_1_obj.Emit(emitParams, 30);
    14.             }

     
    Last edited: Dec 1, 2023
  2. Lenakeiz

    Lenakeiz

    Joined:
    Jun 11, 2015
    Posts:
    34
    Hi @ssaass227 ,

    If you are using a specific frame of reference for your particles I think you need to ensure that the point where you want to emit particles is relative to this custom space. If you have a collision point in world space, you need to convert in the local space and you can do it by using the
    InverseTransformPoint
    .

    So I think you should try something like:

    Code (CSharp):
    1.  
    2. Vector3 collisionPointInCustomSimSpace = referenceTo_For_Particle_SimulationSpace_1.InverseTransformPoint(collision.contacts[0].point);
    3. // Set the position for emitting particles
    4. emitParams.position = collisionPointInCustomSimSpace;
    5.  
    Maybe check that contacts is not empty though.
     
    Bunny83 and ssaass227 like this.
  3. ssaass227

    ssaass227

    Joined:
    Dec 27, 2020
    Posts:
    17
    Thank you very much @Link0n3 , everything works perfect .:)
    Had no idea how
    InverseTransformPoint
    works, now I'll learn and practice it more .
    i see, I just simplefied so it is easier to read.

    thx again
     
  4. Lenakeiz

    Lenakeiz

    Joined:
    Jun 11, 2015
    Posts:
    34
    Nice! Glad to hear it worked!