Search Unity

Parenting: Child's New Rotation Problem [SOLVED]

Discussion in 'Entity Component System' started by Plummers, May 7, 2020.

  1. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35
    Hi, [warning : quaternions!]

    So I am setting an arrow to the be the child of a human, so that the arrow is stuck in the human and rotates with him when he moves. My problem is trying to set the rotation once the arrow has been made a child.

    Setting Rotation:
    ecb.SetComponent(Arrow, new Rotation { Value = math.mul(LocalToWorlds[Arrow].Rotation, math.inverse(LocalToWorlds[Human].Rotation)) });

    Setting Position:
    ecb.SetComponent(Arrow, new Translation { Value = LocalToWorlds[Arrow].Position - LocalToWorlds[Human].Position });


    Sadly this doesn't produce the rotation expected, I think the translation is correct.

    Any help much appreciated, thanks.
     
  2. KwahuNashoba

    KwahuNashoba

    Joined:
    Mar 30, 2015
    Posts:
    110
    Not sure what are you trying to do. What is the result?
    Why not setting Rotation to quaternion.zero and let transform systems do their job?
    Also, try changing order of quaternion multiplication, it's important in some cases.
     
  3. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35
    Thanks but that didn't work, or I'm not exactly sure what you meant. Fortunately found a solution now.
     
  4. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35
    Solution:

    Where B is the projectile, an arrow for example and A is the body it is hitting and sticking in to. Var d makes sure that the scale is properly resolved too.

    Code (CSharp):
    1. var a = new RigidTransform(LocalToWorlds[entityA].Value);
    2. var d = math.inverse(a);
    3. var b = new Translation { Value = math.transform(d, LocalToWorlds[entityB].Position) };
    4. var c = new Rotation { Value = math.mul(d.rot, LocalToWorlds[entityB].Rotation) };
    5. ecb.SetComponent(entityB, c);
    6. ecb.SetComponent(entityB, b);
     
    florianhanke likes this.