- Home /
Avoid scaling GameObject after parenting
Hello Community,
I have a GameObject (Plate) with the Scale (X=13 / Y=1 / Z=7)
and a GameObject (Item) with the Scale (X=1 / Y=0.05 / Z=1)
Now I have Script on the Plate (AttachItemScript)
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Item")
{
other.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Item")
{
other.gameObject.transform.parent = null;
}
}
The Plate is moving and the item should moved too, (OnTriggerEnter).
It's possible, that an item can leave the plate (OnTriggerExit).
The Problem is, sometimes the Item exit the plate and the scale of the item is changing.
How can I avoid this?
@Paski-7 When looking at your code it looks like you try to set the transform of the parent of the colliding item to its parent´s transform(which includes rotation, scale and position) and then trying to nullify it when it exits the collider. It would be helpfull if you could post the purpose of the script. Then I can surely help you.
@mscardinal thanks for your comment - The purpose of this code is: if an object (item) lands on the plate (the plate is moving), the item should move just like the plate. If somethink grab the item from the plate, the item should no longer be a child from the plate!
I see so do you have a trigger collider on the plate or a real?
$$anonymous$$ake a script and put it on the item. Write: public Transform transform;
void Start() { transform = this.gameObject.transform; } void Update() { transform.localScale = new Vector3 ($$anonymous$$athf.Clamp(transform.localScale.x, 1f,1f),$$anonymous$$athf.Clamp(transform.localScale.y, 0.5f,0.5f ), $$anonymous$$athf.Clamp(transform.localScale.z, 1f,1f); }
could work but there can be some errors so just contact me :)
Answer by Magso · Jan 19, 2020 at 02:27 PM
Divide the item's scale by the plate's scale as it's set as the parent.
item.transform.localScale = item.transform.localScale / plate.transform.localScale;