x


Scaling issues.

Basically I'm using the below code to rescale my character according to the power ups the user picks up (think super mario). So for example this makes the character larger. The problem I'm having is that I want to resize the character to a fixed size whereas this seems to scale it relatively. So for example if the user is small and they pick up this power up then they are scaled up but aren't anywhere as big as if they were medium sized when they picked it up.

var newScale : Vector3 = Vector3(20,20,20);
transform.localScale = Vector3.Lerp (transform.localScale, newScale,Time.deltaTime); 

How might I do it so it uses fixed sizes?

more ▼

asked Aug 17 '10 at 08:04 PM

ROM gravatar image

ROM
262 46 49 58

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Try this scale demo. Put it on a cube or something. As you press Fire1 (left mouse button), the object change scale from 1 to 2 and back again next time. It uses coroutines to interpolate scale between two values with an adjustable speed.

var speed : float = 5.0f;

function Start()
{
    yield Demo();
}

function Demo()
{
    while (true)
    {
        yield WaitForButton("Fire1");
        yield ScaleRoutine(transform.localScale, Vector3.one * 2.0f, speed);
        yield WaitForButton("Fire1");
        yield ScaleRoutine(transform.localScale, Vector3.one, speed);
    }
}

function WaitForButton(button : String)
{
    while (!Input.GetButtonDown(button))
        yield;
}

function ScaleRoutine(fromScale : Vector3, toScale : Vector3, speed : float)
{   
    // Basic error handling.
    if (speed <= 0)
    {
        Debug.LogError("speed must be > 0 or algorithm would loop forever");
        return;
    } 

    var scale = fromScale;
    while (transform.localScale != toScale)
    {
        var move : float = speed * Time.deltaTime;
        scale = Vector3.MoveTowards (scale, toScale, move); 
        transform.localScale = scale;
        yield;
    }
}
more ▼

answered Dec 15 '10 at 04:19 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

(comments are locked)
10|3000 characters needed characters left

This function needs to be in Update(), then it sould work. The Lerp will interpolate any value in localScale smoothly to the new absolute value "newScale". The only thing than can influence this is (assuming you don't have any stray scripts messing with your values), that any of the parents have scales other than 1 that keep changing.

more ▼

answered Aug 17 '10 at 09:04 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

localScale is absolute; it's not relative. Using Vector3.Lerp like that won't accomplish anything, however. The third parameter in Lerp is a float between 0 and 1, so assuming you want an animation of the scaling, you need to make a routine that animates the third parameter from 0 to 1 over time. See this for examples of coroutines which you could easily adapt to use localScale rather than position or rotation.

more ▼

answered Aug 17 '10 at 09:15 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

But deltaTime is >0 (although not moving from 0 to 1), and people "misuse" a Lerp all the time for this kind of smooth interpolation. Since he is feeding the current scale into the lerp, it should work (although he might want to add a scaling factor to Time.deltaTime, to influence the smoothing speed).

Aug 17 '10 at 09:28 PM Wolfram

@Wolfram: that kind of misuse results in a "slowdown" factor, which is often not desirable. Using a coroutine to explicitly animate between 0 and 1 allows you to control exactly what sort of curve there should be, if any, and you are guaranteed to end up exactly at the final size.

Aug 17 '10 at 10:35 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x388
x286
x33

asked: Aug 17 '10 at 08:04 PM

Seen: 1274 times

Last Updated: Aug 17 '10 at 08:04 PM