How do you scale an object repeatedly between a max and min scale value over a time interval smoothly? (as in no jumps or jitters).
I am new to both Unity and scripting.
How do you scale an object repeatedly between a max and min scale value over a time interval smoothly? (as in no jumps or jitters).
I am new to both Unity and scripting.
Here, this should get you started.
var amplitude : float;
var scale : Vector3;
function Update ()
{
for (i = 0; i < 3; ++i) scale[i] = amplitude * Mathf.Sin(Time.time) + 1;
transform.localScale = scale;
}
Very cool!
I will try that and let you know.
Thanks so much!
Ok thanks!
It means look at my post again. I’m fixing it. ![]()
I appreciate you fixing it.
Can you tell me what that error means and why it occured… so I can learn something about scripting?
Thanks!
That worked!
Is there a way I can change the amplitude with a max and min variable? I don’t want it going to 0 size.
Thanks again!
Here is the final version that i tweaked successfully to have max and min.
var maxAmp : float;
var minAmp : float;
var scale : Vector3;
function Update ()
{
for (i = 0; i < 3; ++i) scale[i] = maxAmp + minAmp * Mathf.Sin(Time.time) + 1;
transform.localScale = scale;
}
Can you please explain what the function is doing? What is the i variable? where it says i=0; i < 3; ++i) … what is that all about?
Sure. Transform.localScale is a Vector3, which is three floats grouped together. I tried to assign a float to it, like this:
transform.localScale = amplitude * Mathf.Sin(Time.time) + 1;
I know that’s very wrong. However, I try to never use non-uniform scaling, because it can cause problems when parenting and rotation are combined. Therefore, localScale, as far as I’m concerned, is a float. I think that’s why my brain shut down that piece of my knowledge temporarily. :roll:
It’s cool that you already started tweaking that to your liking. Here’s a version with more control. See if it’s to your liking. It doesn’t use a max and min; instead, you define a “base” scale, and the ratio parameter makes it either that many times bigger or smaller at the max change – it will never hit zero, and the scale change is rationally symmetrical.
var rate : float;
var midScale : float;
var ratio : float;
// Drag this game object onto this variable slot.
// It makes the code run faster than using "transform" by itself.
var thisTransform : Transform;
private var scale : Vector3;
function Update ()
{
var scaleComponent = midScale * Mathf.Pow(ratio, Mathf.Sin(Time.time * rate));
for (i = 0; i < 3; ++i) scale[i] = scaleComponent;
thisTransform.localScale = scale;
}
Well, that’s a for loop, a very basic element of all programming. I won’t define that for you, because it’s super easy to look up, and has surely been explained better elsewhere. But the reason we need to use a loop is due to that localScale being a Vector3 issue. Once you know what a for loop does, You can see in this page of the manual, that assigning the same thing, using that loop, is an efficient way to change the uniform scale of an object.
It is not working in C#. I am new bie. Could any one can give it in C#…
Here’s the conversion…some fun learning things added:
using UnityEngine;
using System.Collections;
public class Siny : MonoBehaviour {
float rate;
float midScale;
float ratio;
// Drag this game object onto this variable slot.
// It makes the code run faster than using "transform" by itself.
public Transform thisTransform;
//alternatively, if it is the owning object that gets the scale changes, you can get it automatically, on Start
//this checks to see if it the variable thisTransform is assigned, and if it isn't, assigns it to the current transform.
void Start(){
if(thisTransform==null){
thisTransform=transform;
}
}
private Vector3 scale;
void Update ()
{
float scaleComponent = midScale * Mathf.Pow(ratio, Mathf.Sin(Time.time * rate));
for (int i = 0; i < 3; i++) {
scale = scaleComponent*Vector3.one;
thisTransform.localScale = scale;
}
}
//Alternatively, you could use Mathf.PingPong...
void PingPong(){
thisTransform.localScale=Mathf.PingPong(Time.time*rate,ratio)*Vector3.one;
}
}
I once read somewhere why not to use Time.time for these kinds of calculations in a Update.
It was something like as you keep playing the game… the calculation overhead increases since the value of Time.time gets bigger.