- Home /
Question by
unity_S7sP1OHbAXCRuA · Jul 27, 2019 at 07:49 PM ·
scripting problemscript.scalescalingscaledown
Cannot reduce localScale to 0
Hi!
I've created a sphere that looks like a force field and I was trying to make is swell and then implode until it disappears. So far I have this script:
using UnityEngine;
public class ForceField : MonoBehaviour
{
public Vector3 explosionSpeed;
public Vector3 implosionSpeed;
bool tooBig;
// Start is called before the first frame update
void Start()
{
gameObject.transform.localScale = Vector3.zero;
}
// Update is called once per frame
void Update()
{
if (gameObject.transform.localScale.x > 5 && !tooBig)
{
tooBig = true;
}
if (!tooBig)
{
gameObject.transform.localScale += explosionSpeed;
} else if (gameObject.transform.localScale.x > -0.25 && tooBig)
{
gameObject.transform.localScale -= implosionSpeed;
} else
{
gameObject.transform.localScale = Vector3.zero;
}
}
}
This works until the moment during which the sphere should disappear or have scale 0. At the end it just starts swelling again and then imploding.
I would like it to just reach scale 0 and stay there.
Thanks in advance.
Comment
Best Answer
Answer by benji123abc · Jul 27, 2019 at 08:46 PM
If you change the gameObject.transform.localScale = Vector3.zero;
to gameObject.setActive(false);
you should get the desired result. You may also want to change the gameObject.transform.localScale.x > -0.25
to gameObject.transform.localScale.x > 0
. I hope this helps.