Note that when I say following the child game object, I mean that when the child game object moves, rotates, and stops moving and rotating, the parent game object performs the above actions at the same time. If I stop the child game object from moving, the parent game object must stop moving immediately. The following script does not meet my expectations. The game object (parent game object) attached to this script does not stop immediately. The way I move the child game object is just to drag it in play mode. When I stop dragging, the parent game object does not stop.
If possible, please practice it and make sure you understand what I am talking about before solving the problem.
public class Test6 : MonoBehaviour
{
private void Update()
{
var cTransform = transform.GetChild(0).transform;
gameObject.transform.position = cTransform.position;
gameObject.transform.rotation = cTransform.rotation;
cTransform.position = gameObject.transform.position;
cTransform.rotation = gameObject.transform.rotation;
}
}
1 Like
In the current state, you are trying to block what Unity must do: the child must follow the parent.
Try to unparent it saving the logic of “follower” code.
Sorry, this hierarchy cannot be changed, this is one of the requirements.
1 Like
You may want to elaborate on why this is a requirement. Nonetheless trying to do this is basically going against how transform hierarchies work.
To make a parent follow a child, you would have to monitor any movement in the child, move the parent by the same amount, then subtract that movement from the child to prevent its movement being double by having it parent being moved. All while trying to prevent a feedback loop that goes infinite and probably crashes Unity.
You can probably make these two objects siblings with a common parent. Nonetheless, a rethink to your approach may be in order.
2 Likes
I agree with @spiney199 above. There is almost certainly another hierarchy structure you could use that still allows you to achieve your goal. If that includes getting object A to follow object B, then easy enough. If you specifically want a parent object to follow the child object as you’ve described above, then you going against the exact function of how the hierarchy was designed.
1 Like