Search Unity

SetParent on moving entities does not correctly set position

Discussion in 'Scripting' started by Alexaroth, Apr 29, 2019.

  1. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29
    Have this simple script to set a decal projector against a moving cube:


    Code (CSharp):
    1. decal.transform.SetParent(hit.collider.transform);
    2. decal.transform.position = hit.point;
    3. decal.transform.Translate(hit.normal * 0.02f, Space.Self);
    4. decal.transform.rotation = Quaternion.FromToRotation(-Vector3.forward, hit.normal);
    Issue is that if the RaycastHit hit came from me shooting the ray at the cube moving towards me, then the position of the decal will be inside the cube, and as such, not visible. If I shoot it to the side (0 speed towards me), then it works. I tried putting the raycastHit trigger (happens when I click on something) in LateUpdate, that helped a bit but the decal is still inside the cube (not as much tho).

    So, what is happening? does the cube move before the setParrent call actually implements the 'attachment'? How can I fix this?

    PS: Cube I am raycasting against moves using a navmesh agent


    EDIT: SOLUTION in my reply here: https://forum.unity.com/threads/set...t-correctly-set-position.669784/#post-4497775
     
    Last edited: May 3, 2019
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'm not sure if this will help, but try the following:
    Code (CSharp):
    1. decal.transform.position = hit.point + hit.normal * 0.02f; // move it slightly above the face
    2. decal.transform.rotation = Quaternion.LookRotation(hit.normal, hit.collider.transform.up);
    3. decal.transform.SetParent(hit.collider.transform);
    Oh, if you using a projector instead of affixing a small quad, LookRotation should probably be -hit.normal)
     
    Last edited: Apr 29, 2019
  3. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29

    Sadly no, it only seems to project the decal from within the object. Works the same when shooting static objects, but when shooting a cube moving towards me it's still too far into it to project it properly on it's surface. Tried putting this code in fixed update as well, doesn't fix it
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hmm. what do you mean 'too far in the object? The projector should be 0.02 above the surface. what's your near plane clip setting? Since it's a projector, perhaps all you need is changin that 0.02f to 1.0f (make it stick out 1.0 m outside the cube, facing the cube) and adjust the near plane in the projector to 0.1?
     
  5. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29

    What I mean by too far in is that the hit point position returned by the raycast test is already inside the moving object, not on the surface of the cube:

    upload_2019-4-29_16-28-54.png

    I believe I am getting the hitpoint from the cube's transform from its previous frame, if that makes sense

    Logged z positions (cube moving on Z, towards me) here is what I got in a test case:

    ray hit point Z = -0.7426137 cube position Z = -1.184865

    Cube size is 1 by 1 by 1, which means that on Z, the ray's hit point should be at cube pos z - 0.5, in the above case -1.184865 - 0.5 = -1.684865 instead it's at -0.7426137
     
    Last edited: Apr 29, 2019
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hmmm. Strangely enough (I don't know how and when you took that screenie), your projector's transform shows 0,0,0 -- that's a very unlikely coincidence. Are you sure you are parenting correctly?

    -ch
     
  7. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29
    yeah that's always 0 0 0, the decal is a prefab with a base empty object, and what I clicked on was the projector that is a child object of that empty object, at 0 0 0
     
  8. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29
    Reduced the speed of the moving object, and the hit point returned by the raycast was closer to the actual side of the cube (still inside), thus confirming that when you have objects moving along the direction of the raycast (cube coming towards camera), the hit point will be inaccurate as the cube has moved since the internal collision calculations were made
     
  9. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29
    Submitted bug about this. Still occurring in a simple test project with a navAgent cube moving towards the camera and me just raycasting against it. The hit point is inside the cube
     
  10. Alexaroth

    Alexaroth

    Joined:
    Feb 4, 2019
    Posts:
    29
    For anyone that ever stumbles upon this through some convoluted google search, here is the response I got from the Unity team from the bug report:

    Hi,

    Thank you so much for sending the project. After an investigation, this issue turned out to be by design. At the moment you are shooting a raycast, Physics Engine does not know the exact position of the character in the new frame. Therefore, you need to Sync Transforms. You can do that in two ways:
    1) Manual: in GAME.cs line 53 add a line Physics.SyncTransforms();
    https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html

    2) Automatic: in Physics settings, Edit > Project Settings > Physics Settings, tick Auto Sync Transforms.
    https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html



    Hope this helps anyone else struggling with this!