I created a function that parents the Sticky Bomb to the first gameobject it hits on the layers. It casts an OverlapSphere until it finds the object.
public void detectClosestObj()
{
/* Code below checks for the closest object only on the bullet and enemy unit layer
* it will parent to the first collided object
*/
LayerMask layerMask = (1 << 10 | 1 << 16);
Collider[] checkObj = Physics.OverlapSphere(this.gameObject.transform.position, 0.1f, layerMask);
List<GameObject> cols = new List<GameObject>();
foreach (Collider hit in checkObj)
{
Debug.Log("hit : " + hit.name);
RaycastHit rayHit;
if (hit.tag != "Floor")
{
this.gameObject.transform.SetParent(hit.gameObject.transform,true);
}
else if (hit.tag == "Floor")
{
stuckToObject = true;
}
break;
}
This works fine if the scale of the objects are 1,1,1 or 100,100,100.
But when the scale of the object is different, it stretches the sticky bomb creating weird shapes?
I thought the line below made the object keep its relative scale, transform and rotation?
Personally i just use the “parent” property and never had any issues with it.
The SetParent Method - in my books - is only needed if you want to parent UI elements (Rect Transforms)
When I’ve had similar problems, I always found that the best (though not always quickest) solution was to rework where my non-identity scaling was in the hierarchy. I find that it’s best for such scaling to be as far down into the hierarchy as possible.
In your case, is the parent scaled because it has a mesh/sprite/collider attached? If so, I’d recommend creating a child under the parent, moving the mesh/sprite/collider to the child object, and scaling the child instead of the parent. The parent can keep any non-spatial components that don’t need to be on the same object as those components that got moved to the child. That way, any additional children added to the parent can have their own distinct scaling, and don’t get messed up by the scaling of the parent or any of the other children.