This is how I'm going about it:
if(transform.localScale.x > 7)
{
for(int i = 7; i > 1; i--)
{
transform.localScale -= new Vector3(1, 1, 1) * Time.deltaTime;
}
}
else
{
transform.localScale = new Vector3(1, 1, 1) * Time.deltaTime;
}
The else statement is executed to increase the scale and when it reaches 7 the if statement should decrease it back to 1 thanks to the for loop which should run for 6 iterations and decrease the scale 6 times. But only the else part works properly when the scale reaches 7 it just gets decreased to 6 and then back to 7 in a second. It doesn't go all the way down to 1.
CodePudding user response:
You know that your entire for
loop will be executed immediately within one single frame? and you multiply that by Time.deltaTime
which assuming 60 frames per second would be a value around 0.0166666666...
.
So what you are ding in a loop basically just equals doing
transform.localScale -= Vector3.one * 6 * 0.017f;
Right in the next frame you might already again be lower than 7
=> you land in the else
case and again increase the scale.
So in the next frame it will already decrease it again etc
From what I understand is you rather want kind of a pin pong effect between two scales.
I would rather use something like
// adjust via the Inspector
public float speed = 1;
public float amplitude = 7f;
private Vector3 originalScale;
private float timePassed;
private void Start()
{
originalScale = transform.localScale;
}
private void Update()
{
timePassed = Time.deltaTime * speed;
// see https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html
transform.localScale = originalScale * (1 Mathf.PingPong(timePassed, amplitude - 1));
// or with ease-in and ease-out
//transform.localScale = originalScale * (1 (amplitude - 1) * Mathf.SmoothStep(0, 1, Mathf.PingPong(timePassed, 1)));
}
Where Mathf.PingPong
returns a value that will increment and decrement between the value 0 and length.
Or if you actually need a different increase and decrease duration I would rather suggest a Coroutine like
// adjust via the Inspector
public float increaseDuration = 1f;
public float decreaseDuration = 7f;
public float amplitude = 7f;
private IEnuemerator Start()
{
var originalScale = transform.localScale;
var maxScale = originalScale * amplitude;
while(true)
{
for(var timePassed = 0f; timePassed < increaseDuration; timePassed = Time.deltaTime)
{
var factor = timePassed / increaseDuration;
// optinal add ease-in and ease-out
//factor = Mathf.SmoothStep(0, 1, factor);
transform.localScale = Vector3.Lerp(originalScale, maxScale, factor);
yield return null;
}
for(var timePassed = 0f; timePassed < decreaseDuration; timePassed = Time.deltaTime)
{
var factor = timePassed / decreaseDuration;
// optinal add ease-in and ease-out
//factor = Mathf.SmoothStep(0, 1, factor);
transform.localScale = Vector3.Lerp(maxScale, originalScale, factor);
yield return null;
}
}
}
CodePudding user response:
You can make an other variable like:
public bool isDecreasing=false;
if(!isDecreasing && transform.localScale.x > 7)
{
isDecreasing=true
}else if(isDecreasing&&transform.localScale.x==1)
{
isDecreasing=false
}
if(isDecreasing)
{
transform.localScale -= new Vector3(1, 1, 1) * Time.deltaTime;
}else
{
transform.localScale = new Vector3(1, 1, 1) * Time.deltaTime;
}