- Home /
How to reset GameObject to original scale
Hello Guys! I am making a game like Agar.io, as you know the when the player eats food(dots), it grows, it is not the problem but when the player gets small(by throwing food from itself), I don't want it to get smaller than it's original scale. So Please Help! I hope you understand my question, if not, then tell me I will try to explain more.
Thanks in Advance!
Can you provide the code that you currently have for the growing, and decreasing of the scale that you currently have. Then we can adjust it to fit your requirements.
{ public float Increase; public float Decrease; void Start() { } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Food")) { transform.localScale += new Vector3(Increase, Increase, 0f); // value of increase = 0.2 Destroy(other.gameObject); } } void Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
transform.localScale += new Vector3(Decrease, Decrease, 0f); // value of decrease =
0.5
}
} }
I don't know why it is showing like that but I have commented below.
Thanks for your answer, it kinda helped but there is still a problem, that I don't want it to directly change to its original scale, like, I want it to make it little smaller (like 0.5(decrease) through the variable) when I press the L$$anonymous$$B. I hope you understand. Thanks!
Answer by ArsalanP · Jun 08, 2020 at 06:30 PM
{
public float Increase;
public float Decrease;
void Start()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Food"))
{
transform.localScale += new Vector3(Increase, Increase, 0f); // value of increase = 0.2
Destroy(other.gameObject);
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.localScale += new Vector3(Decrease, Decrease, 0f); // value of decrease = 0.5
}
}
}
Answer by ShadyProductions · Jun 08, 2020 at 09:09 PM
private Vector3 _originalLocalScale;
private void Start()
{
_originalLocalScale = transform.localScale;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
var toChange = _originalLocalScale + new Vector3(Decrease, Decrease, 0f);
if (toChange.x >= _originalLocalScale.x && toChange.y >= _originalLocalScale.y)
{
transform.localScale = toChange;
}
}
}
This should do the trick. @ArsalanP