- Home /
How to Scale an object up and down over time?
Hi,
I have this code that should scale an object up and down over a series of time.
My problem is however that it reaches its max scale and does not set the boolean to switch the statement to scale down.
if (transform.localScale.x < mMinScale.x && !mScaleUp) //
{
transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
print("HELLO");
}
if (transform.localScale.x < mMaxScale.x && mScaleUp)
{
transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
print("GOODBYE");
}
if (transform.localScale.x == mMaxScale.x)
{
mScaleUp = false;
print(mScaleUp);
}
Any ideas?
Kind Regards
New code is
if (transform.localScale.x > mMinScale.x && !mScaleUp)
{
transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
print("HELLO");
}
if (transform.localScale.x < mMaxScale.x && mScaleUp)
{
// transform.localScale = //Time.deltaTime
transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
print("GOODBYE");
}
if (transform.localScale.x >= mMaxScale.x)
{
mScaleUp = false;
print(mScaleUp);
}
else if (transform.localScale.x <= mMinScale.x)
{
mScaleUp = true;
print(mScaleUp);
}
maxScale is 1 but the highest the transform gets to is 0.9999992
Answer by RicardoAntezana · Aug 10, 2017 at 11:43 AM
Hey!
Why not try using the Sin function to create a repetitive motion such as a pulsating object? I feel this is a perfect opportunity to use one!
In this example script we will have your transform object to have its size move from 1 to -1:
using UnityEngine;
public class Scaling : MonoBehaviour {
void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time), Mathf.Sin(Time.time), Mathf.Sin(Time.time));
transform.localScale = vec;
}
}
If you do not want your object to go from -1 or 1 then you can add numbers to your sin function like so:
using UnityEngine;
public class Scaling : MonoBehaviour {
void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1);
transform.localScale = vec;
}
}
That script will make your object grow from 0 to 2 and then back again.
You can change the speed at which it pulsates by multiplying your time variable by some number, for example:
using UnityEngine;
public class Scaling : MonoBehaviour {
void Update ()
{
Vector3 vec = new Vector3(Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2));
transform.localScale = vec;
}
}
That script would make it pulsate at twice the speed! Aren't Sin functions great? You could also make it 0.5 or a smaller fraction to make it pulsate slower.
You can change the amplitude of your cube by multiplying the entire function by some number, for example:
using UnityEngine;
public class Scaling : MonoBehaviour {
void Update ()
{
Vector3 vec = new Vector3(2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time));
transform.localScale = vec;
}
}
That script would make your transform pulsate from -2 to 2. If you were to multiply it by 0.5 or a smaller fraction, it would pulsate at a smaller pace.
Here is a great video to learn more about Sin functions: https://www.youtube.com/watch?v=pEXdTLsEAjk
Thanks, I hope you learned something and good luck with your programming endeavours!
Answer by FunIsDangerous · Jul 04, 2017 at 02:10 PM
This seems to work perfectly. If you dont want it to scale on a certain axis, simply set the scaling speed on that axis to 0.
[Header("Max scale")]
public Vector3 mMinScale;
[Header("Min scale")]
public Vector3 mMaxScale;
[Header("Scaling Speed")]
public Vector3 mScaleSpeed;
private Vector3 currentScaleSpeed;
void Start()
{
currentScaleSpeed = mScaleSpeed;
}
void Update()
{
if (transform.localScale.x <= mMinScale.x || transform.localScale.x >= mMaxScale.x)
{
currentScaleSpeed.x *= -1; //invert it
}
if (transform.localScale.y <= mMinScale.y || transform.localScale.y >= mMaxScale.y)
{
currentScaleSpeed.y *= -1; //invert it
}
if (transform.localScale.z <= mMinScale.z || transform.localScale.z >= mMaxScale.z)
{
currentScaleSpeed.z *= -1; //invert it
}
transform.localScale = transform.localScale + currentScaleSpeed * Time.deltaTime;
}
Thanks for your comment I'll try to implement this tomorrow :)
Answer by Saryk360 · Jun 22, 2017 at 03:13 PM
Change "==" to ">=" on line 12.
No change, does exactly the same. I understand why you suggested that though. I'll keep it that way in the eventuality it is >= than. :)
Hey,
I never managed to get it to work, I tried adding a statement like your comment below but I couldn't get it to work.
I understand the logic you was trying to add but I'm lost as to why it doesn't work I benched it for a while but I will try and pick it up again tomorrow.
$$anonymous$$any Thanks
So basically you should change the "<" on line 1 to a ">", the "==" to a ">=" on line 12, add a statement to reset mScaleUp to true when "transform.localScale.x <= m$$anonymous$$inScale.x", and then I don't think there's any reason for it not to work :)
Answer by Manpreet_96 · Jun 22, 2017 at 05:42 PM
On line 1, change "if (transform.localScale.x < mMinScale.x && !mScaleUp)" to "if (transform.localScale.x > mMinScale.x && !mScaleUp)".. Rest looks fine, it should work.. @littlebigprogramming
Tried that also, sadly it made no difference. Thanks for the suggestion. :)
@littlebigprogram$$anonymous$$g To the best of my knowledge, your both if statements are being run. Change the condition in first "if" as i suggested before and change the second "if" statement to "else if". Do comment if this works, otherwise I have another approach too in $$anonymous$$d but that would be a bit lengthy. $$anonymous$$eep Chillin!!! :D
Answer by NiaG-A · Aug 19, 2017 at 08:49 AM
I made a tutorial for this on my YouTube channel https://www.youtube.com/watch?v=Wymchsif758 it only requires a couple lines of code
Your answer
Follow this Question
Related Questions
How to use WaitForSeconds inside IF / Else If statement? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scale Object Using Lerp? 2 Answers
problem Changing Scales in Script -C# 4 Answers