Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

SetParent() on object from Physics.OverlapSphere sets wrong position

$
0
0
When I use SetParent on a transform that I get from Physics.OverlapSphere, the position is not set correctly. The new child gets offset by the same numerical offset as the Parent world position it seems. If I assign the child transform (or Collider) to a public variable SetParent works as expected. In the Editor it looks like the child moves to the correct position first and then immediately gets offset. Debug.Log only shows one call to SetParent(). **This only happens when I use the collider from OverlapSphere, not if I manually assign the same collider to a variable.** I have checked with Debug.Log and it looks like it's getting the correct GameObject, Transform, Collider etc. This code is run in Update on the parent object: Collider[] overlaps = Physics.OverlapSphere(socket.position, 0.1f); foreach (Collider item in overlaps) { if (item.gameObject.GetComponent()){ item.gameObject.transform.SetParent(socket, true); break; } } "socket" is an empty child of the parent, where the object should get attached. The Grabbable script is just an empty class. So before the attach it looks like this: ItemObject (Cylinder with Capsulecollider set as trigger) ParentObect (Cube with rigidbody) > SocketObject (Empty gameobject) And after the attach triggers it looks like this: ParentObject > SocketObject > ItemObject ItemObject should be centered in SocketObject, but it gets offset instead. Am I doing something wrong, or why does it behave like this? I don't think the code in general is wrong, since *the problem only manifests if I use Physics.OverlapSphere*, not if I assign the same collider to a public field.
Edit: v 2019.4.22f1

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>