Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a sprite to shrink and grow on command but can't figure out how. Any help would be appreciated. This is my current code.
transform.localScale += new Vector3(0.2f,0.2f,0.5f);Input.GetAxis("Fire2");


What I have tried:

Still pretty new to C#, can't figure out how to write it.
transform.localScale(Input.GetAxis("Fire2"), 0.2f,0.2f);
transform.localScale.System.Collections(Vector3)(0,0 Input.GetAxis("Fire2")
transform.localScale(Input.GetAxis("Fire2");
Posted
Updated 28-Jul-20 5:20am
v3
Comments
Afzaal Ahmad Zeeshan 24-Jul-20 22:23pm    
And the current problem is?
Member 14898413 25-Jul-20 12:15pm    
When I run, transform.localScale += new Vector3(0.2f,0.2f,0.5f); Input.GetAxis("Fire2") the sprites start growing and never stop and it is not on command.

1 solution

You say you want it to grow and shrink but you don't specify when\how it grows and when\how it shrinks. If you want to grow on Fire2 and shrink on Fire1 then you'll need code like

C#
var f1 = Input.GetAxis("Fire1");
var f2 = Input.GetAxis("Fire2");

if (f1 > 0)
{
    transform.localScale += new Vector3(0f, 0f, -0.01f * f1);
}
else if (f2 > 0)
{
    transform.localScale += new Vector3(0f, 0f, 0.01f * f2);
}


As well as having different conditions for growing and shrinking I've multiplied the axis value by 0.01 as the fire buttons return 0 or 1 and growing by a factor of 1 is quite extreme. Technically you could just hard-code the value as multiplying by 1 does nothing, but I'm assuming controls with analogue fire buttons like the xbox and ps4 might return a value between 0 and 1 depending on how far it is depressed.

If you want the sprite to "bounce", ie to grow to a certain size then shrink again and so on you'll need something like this;

C#
private float scaleChange = 0.01f;

void Update()
{
    var f2 = Input.GetAxis("Fire2");

    if (f2 > 0)
    {
        // Fire2 is held so scale the z axis by the scaleChage value
        transform.localScale += new Vector3(0f, 0f, scaleChange * f2);

        // check if we have reached the upper or lower bound of the scale
        if (transform.localScale.z >= 1.5 || transform.localScale.z <= 0.5)
        {
            // we are either at the max scale or the min scale so reverse the scaling by multiplying it by -1
            // if we were growing before we are now shrinking and vice versa
            scaleChange = -1 * scaleChange;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900