- Home /
How to stop scale reducing from reaching Zero (Solved)
Im using a very simple script to make an object get smaller over time
#pragma strict
function Update()
{
transform.localScale -= Vector3(0.002,0.002,0.002); // Reduce object size by .002 per frame
}
Now think of it like iron ore being mined, eventually the object will reach scale 0 once its all been mined. It's at this point I want the reduce scaling to stop. (at the moment it goes into negative figures and grows again)
How can I set the object(objects scale) a value or percentage? I don't want it to reach less than zero, better still be able to stop it at about 2% of the original size.
Do I have to assine an INT/Float value to each of the vector3 x,y,z co-ordinates (and if so... how?)
Or is there a more efficient way to do this?
The script so far now is-
#pragma strict
var beingMined : boolean = false;
function Update()
{
if(beingMined == true)
{
transform.localScale -= Vector3(0.002,0.002,0.002); // Reduce object size by .002 per frame
}
}
As usual any help/suggestions is greatly appreciated.
you could do:
transform.localScale -= Vector3(0.002,0.002,0.002);
transform.localScale = Vector3.$$anonymous$$ax(Vector3(0.02, 0.02, 0.02), transform.localScale);
Answer by HarshadK · Jan 14, 2015 at 02:07 PM
One possible way is to do this:
#pragma strict
var beingMined : boolean = false;
function Update()
{
if(beingMined == true)
{
// Check if the local scale of the object is greater than zero on all axes. And if yes, then only decrement
if(transform.localScale.x > 0 && transform.localScale.y > 0 && transform.localScale.z > 0 )
{
transform.localScale -= Vector3(0.002,0.002,0.002); // Reduce object size by .002 per frame
}
// This check makes sure that the scale does not reach below zero
if(transform.localScale.x < 0 && transform.localScale.y < 0 && transform.localScale.z < 0 )
{
transform.localScale = Vector3.zero;
}
}
}
Certainly looks better than my attempt -
#pragma strict
var being$$anonymous$$ined : boolean = true;
var oreAmount : int = 1;
function start()
{
//transform.localScale = Vector3(oreAmount,oreAmount,oreAmount);
}
function Update()
{
transform.localScale = Vector3(oreAmount,oreAmount,oreAmount);
if(being$$anonymous$$ined == true && oreAmount >= 0)
{
transform.localScale -= Vector3(0.002,0.002,0.002); // Reduce object size by .002 per frame
}
else
{
being$$anonymous$$ined = false;
}
}
Tried putting my first statement in the Start function but that didn't do anything (its commented out now).
Tried again with a comment in the function update and the damn ore turned into some kind of giant 1000 times larger and crashed unity...lol. Would be interesting to know why/how it caused that effect ?
You missed another && from your statement, but apart from that it works perfectly.
$$anonymous$$any thanks, much appreciated !
Your answer
Follow this Question
Related Questions
problem Changing Scales in Script -C# 4 Answers
Add a value to a string 1 Answer
Resize object forward instead of along the z axis 4 Answers
Scaling objects over time? 3 Answers
LocalScale not working idealy 1 Answer