Scaling up and down with Time?

Hi, I seem to be having an issue scaling a simple box using Time.deltaTime. The effect I’m going for is a box that spins and gets a bit bigger and then returns to its original size smoothly. But for some reason can not not get the code to loop from big to small, it simply just keeps scaling up. Iv’e been working on it for hours and it’s driving me crazy. Any help would be greatly appreciated!!

Here is my code:

using UnityEngine;
using System.Collections;

public class constantRotation : MonoBehaviour {
	
	public float rotaionspeed = 0;
	public float flux = 2.0f;
	private int flexout = 0;
	private int flexin = 21;
	
	
	
	
	public void Start ()  {
	
		
		
	}
	
	void Update () {
	
		
		transform.eulerAngles += new Vector3(0, rotaionspeed,0); //Constant Rotation
		
		if (flexout <= 20) {
			
			transform.localScale += new Vector3 (flux * Time.deltaTime,flux * Time.deltaTime,flux * Time.deltaTime);
			flexout ++;
			
		}
		
		if (flexout >= 20) {
			
			flexout = 21;
			flexin = 0;
			
		}
		
		if (flexin <= 20){
			
			transform.localScale += new Vector3 (-flux * Time.deltaTime,-flux * Time.deltaTime,-flux * Time.deltaTime);
			flexin ++;
		}
		if (flexin >= 20) {
			
			flexin = 21;
			flexout =0;
		}
	
	}
}

2 Answers

2

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
        transform.localScale = new Vector3(Mathf.PingPong(Time.time, 3), transform.localScale.y, transform.localScale.z);
    }
}

Wow that's incredible! Thank you for your help bchilds88. I have yet to use Mathf, PingPong, or Time.time. It's amazing how much code I was trying to use and you simplified it down to two lines =)I would like to find out what those specifically do. Thanks again!

Glad i Could help! :)

using UnityEngine;
using System.Collections;

public class ScaleUpDown: MonoBehaviour {

    float scalingTime, scalingSpeed = 0.3f, targetScale = 0.3f;
	float length = 0.3f;

	void Update ()
	{
		scalingTime = Time.time * scalingSpeed;  
		transform.localScale = new Vector3 (
			Mathf.PingPong (scalingTime, length) + targetScale, 
			Mathf.PingPong (scalingTime, length) + targetScale, 0
		);
	}
}