Using C# to make an object scale by 3x, wait 5 seconds and then Scale back to normal?

Hello, I’m a complete noob to scripting but I’ve been trying to play with scaling an object via C#

What I’d like to do is make an object scale by 3x, wait 5 seconds and then Scale back to normal at a rate of .25.

So far I’ve gotten it to scale up, but i don’t know of it’s scaling by 3x?

//Scale Object Up...
		
		if  (Input.GetKey(KeyCode.Space) ) {
		transform.localScale += new Vector3(0.3F, .3F, .3F);
			
		}

I’m sure .3 does’t translate to 3x, but I haven’t the faintest idea how to know for sure. And if you happen to know any tips that point point me in the right direction for the rest of the functionality I would be very appreciative. :confused:

Look up coroutines in the docs.

Sage advice, and also give iTween a look too.

I didn’t quite get what effect you’re after. But I’ve written up something similar. Let me know how it works for you.

using UnityEngine;
using System.Collections;

public class ScaleTest : MonoBehaviour {
	
	// This is the time it takes to scale up/down.
	public float duration = 1.5f;
	
	// This remains true while it is in the prossess of scaling.
	private bool isScaling = false;
	
	
	// This is called every frame.  I will use it to check if space is pressed down, then trigger the scale.
	void Update () {
		
		if (Input.GetKeyDown ("space")) {
			StartCoroutine (DoScaleThing ());
		}
	}
	
	public IEnumerator DoScaleThing () {
		
		// Check if we are scaling now, if so exit method to avoid overlap.
		if (isScaling)
			yield break;
		
		// Declare that we are scaling now.
		isScaling = true;
		
		// Grab the current time and store it in a variable.
		float startTime = Time.time;
		
		while (Time.time - startTime < duration) {
			float amount = (Time.time - startTime) / duration;
			transform.localScale = Vector3.Lerp (Vector3.one, Vector3.one * 3.0f, amount);
			yield return null;
		}
		
		// Snap the scale to 3.0f.
		transform.localScale = Vector3.one * 3.0f;
		
		// Leave the scale at 3 for 2 seconds (this can be changed at any time).
		yield return new WaitForSeconds (2.0f);
		
		// Now for the scale down part.  Store the current time in the same variable.
		startTime = Time.time;
		
		while (Time.time - startTime < duration) {
			float amount = (Time.time - startTime) / duration;
			transform.localScale = Vector3.Lerp (Vector3.one * 3.0f, Vector3.one, amount);
			yield return null;
		}
		
		// Snap the scale to 1.0.
		transform.localScale = Vector3.one;
		
		// Declare that we are no longer modifing the scale.
		isScaling = false;
	}
}
1 Like

Alright thanks for the help, I’ll check out your suggestions.

And extra thanks Daniel, seeing how you worked something out is super helpful.

Hey no problem. If you have any questions about how my script works feel free to ask.

I ended up having some hands on help from my teacher. This is what I ended up with…

	public float timer = 5f;
	public float shrink = .25f;
	private bool big = false;
	private Vector3 originalScale;



//This is where it scales up...
		if  (Input.GetKeyDown(KeyCode.Space) ) {
		
		//Declaring the original scale of the object, reguardless of any scaling that occured to the object previous 
		//to the code being activated
		
		originalScale = transform.localScale;
        
		transform.localScale *=3;
		big = true;
		}
		
		//Once it scales up, big becomes true...
		//Once it's true code subtracts timer (5f) from deltatime (1)
		
		if (big) {
			timer -= Time.deltaTime;
			
		//Each time it subtracts from timer, it will check if it hits 0 or below 0
		//Once it hits zero the current local scale (Original Number *3), will be subtracted by .25 (shrink)
			
		if (timer <=0) {
				transform.localScale -= new Vector3 (shrink, shrink, shrink);
				 
			}
			
		//When the transform localscale is equal to objects original scale, then big becomes false and the code
		//stops subtracting from local scale
			
		if (transform.localScale == originalScale) {
				big = false;
				
												 }
				  }
			
		
				
}
		
}

The only thing I’m having a problem with now is that when I’ve hit the spacebar once, it no longer waits for 5 seconds to get smaller if i hit the spacebar again…it just tries to scale down immediately

Alright, I figured out it because the timer doesn’t reset, but how do I reset the timer rawr.

1 Like

Alright resetting the timer was easy once I remembered == is a question!

if (big == false) {
				timer = 5f;
				}

Victory :slight_smile:

1 Like