- Home /
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;
}
}
}
Answer by Triqy · Nov 10, 2013 at 07:57 PM
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 $$anonymous$$athf, 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!
Answer by Musabbir · Sep 24, 2019 at 04:58 PM
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
);
}
}
Your answer
Follow this Question
Related Questions
How to setup a 2D Beat em up? 1 Answer
Screen Resolution Scaling No Solution? 1 Answer
Trouble getting XInput set up 0 Answers
Instantiated object has the scale of the old object that was distroied 0 Answers
Setting up an array of tags. 2 Answers