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. Dismiss Notice

Question Moving parent object to match child

Discussion in 'Scripting' started by Kuera, Sep 14, 2023.

  1. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    If an object cube (parent) has 4 children (basically snap points) on 4 of the faces, how would one go about the logic of getting the cubes to snap together? I was working on the idea of getting a snap point of cube B to lock onto cube As snap point but I'm not sure how to get the cube parent object to move with the child snap point object.
     
  2. irene_reko

    irene_reko

    Joined:
    Feb 14, 2019
    Posts:
    11
    you need to apply the original inverse transform between the cube and the child object. and after that, reset the local pose of the child object.
     
  3. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    I'm not sure I quite follow that.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Honestly, wanting to move an object with children, while keeping children stationary is generally not the best approach.

    Make all these objects siblings, and the process becomes a lot easier.
     
    irene_reko likes this.
  5. irene_reko

    irene_reko

    Joined:
    Feb 14, 2019
    Posts:
    11
    Fully agree with that.

    There is a relative transformation between your cube and your snap point. When you snap the snap point to another cube, this transformation will be changed. The snap point is at the new location, but the cube is still at the old position. To fix that, you have to restore the original transformation between cube and snap point.

    You can get the relative transformation with Transform.InverseTransformPoint and Transform.InverseTransformDirection. Alternatively, you can track all transform changes to the snap point and apply them to the cube as well.
     
    spiney199 likes this.
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohhh, you mean just make the children snap to the parent and stay there? Well, children will stay with the parent regardless, that's the whole point of making something childed. So you're basically asking how to get those snap points?

    Here's a easy tutorial, that will help understanding how to have something "snap" to a particular object:
    Code (CSharp):
    1. public class AddCube : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         if (Input.GetMouseButtonDown(0))
    6.         {
    7.             Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
    8.             if (Physics.Raycast(ray,out RaycastHit hit,500f))
    9.             {
    10.                 Vector3 sideVector = new Vector3(
    11.                     hit.transform.localScale.x * hit.normal.x,
    12.                     hit.transform.localScale.y * hit.normal.y,
    13.                     hit.transform.localScale.z * hit.normal.z);
    14.                 Instantiate(hit.transform.gameObject,
    15.                     hit.transform.position + sideVector, hit.transform.rotation);
    16.             }
    17.         }
    18.     }
    19. }
    Which would be you have just a standard cube in the scene, you click on it, and it creates another cube on the side of "hit". But it's also modified for different scales(x,y,z).

    Now, however, this uses raycasts, so it technically wouldn't apply for what you're looking for(just a helping example).

    But you would just declare what those positions are from the parent, local space, and as you're childing the new objects to the parent, you have them go to that position, then child them. Once they are childed to the parent, they move with the parent(unless coded to do otherwise).

    A off-hand example I can give is similar to my raft-style game:
    Code (CSharp):
    1. void ChildFromHitNormal(Collision col)
    2. {
    3.     Collider other = col.contacts[0].otherCollider;
    4.  
    5.     if (other.transform.TryGetComponent<Raft>(out Raft raft))
    6.     {
    7.         trans.position = raft.trans.position + col.contacts[0].normal;
    8.         trans.SetParent(raft.trans);
    9.         Destroy(rig);
    10.     }
    11. }
    As you can see, I set the position(relative to world space from collision), and then child the object to the parent after that. Hence the child will now stay with the parent from here on out(unless coded to do otherwise).

    So you just simply need to figure how to get your positions, either from world space, or parent position plus your new offset.
     
  7. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    I got the desired effect. In short I made it so when the blueprint object's snap point located a nearby placed objects snap point the script would convert the child to been the parent and move it, therefore dragging the blueprint with it to the desired location. If I move a certain distance away it reverts the whole child/parent relationship. It works surprisingly better than expected.