How can I make that the object will scale automatic up/down nonstop ?

Now I’m using the F key to make it scale up or down. But I want to add another method for example AutoScaling that when calling it in Update it will scale up first once finished scaling up it will scale down and then up again and so no nonstop.

The Scaling script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scaling : UnityEngine.MonoBehaviour
{
   public GameObject objectToScale;
   public GameObject lookAtTarget;
   public float duration = 1f;
   public Vector3 minSize;
   public Vector3 maxSize;
   public bool scaleUp = false;
   public Coroutine scaleCoroutine;
   public bool scalingHasFinished = false;

   public void Inits()
   {
       scalingHasFinished = false;
       objectToScale.transform.localScale = minSize;
   }

   public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, Camera objectToScaleCamera)
   {
       float counter = 0;
       Vector3 startScaleSize = targetObj.transform.localScale;

       while (counter < duration)
       {
           counter += Time.deltaTime;
           targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

           if (scaleUp)
           {
               var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
               lookPos.y = 0;
               var rotation = Quaternion.LookRotation(lookPos);
               objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
           }
           else
           {
               var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
               lookPos.y = 0;
               var rotation = Quaternion.LookRotation(objectToScaleCamera.transform.forward);//SwitchCameras.GetCurrentCamera().transform.forward);//Camera.main.transform.forward);
               objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
           }

           yield return null;
       }

       scalingHasFinished = true;
   }

   public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, float rotationSpeed)
   {
       float counter = 0;
       Vector3 startScaleSize = targetObj.transform.localScale;

       while (counter < duration)
       {
           counter += Time.deltaTime;
           targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

           targetObj.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime, Space.Self);

           yield return null;
       }
   }
}

And the script that use the scaling :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsManipulation : UnityEngine.MonoBehaviour
{
   //Camera
   public Camera playerCamera;

   //Scaling
   private bool canScale = true;
   private Scaling scaling;

   //Lights
   public DimLights dimlights;
   private Coroutine lightCoroutine;

   //Colors
   private Colors colors;

   //Rotating
   private bool stopRotation = false;
   private Rotating rotating;

   private void Start()
   {
       scaling = GetComponent<Scaling>();
       scaling.Inits();

       colors = GetComponent<Colors>();
       colors.Start();

       rotating = GetComponent<Rotating>();
   }

   // Use this for initialization
   void Update()
   {
       if (playerCamera != null)
       {
           //Scaling
           if (Input.GetKeyDown(KeyCode.F) && canScale == true)
           {
               Scaling();
           }
       }

       //Rotate
       if (Input.GetKey(KeyCode.R) && !scaling.scaleUp)
       {
           rotating.x += Time.deltaTime * rotating.rotationSpeed;
           scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
           rotating.keyPressed = true;
       }
       if (Input.GetKeyUp(KeyCode.R))
       {
           rotating.keyPressed = false;
       }

       if (!rotating.keyPressed && !scaling.scaleUp && rotating.rotateBack == false
           && DetectInteractable.detected == false)
       {
           scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
       }

       if (DetectInteractable.detected == true && !scaling.scaleUp && stopRotation == false)
       {
           rotating.x += Time.deltaTime * rotating.rotationSpeed;
           scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
       }
   }

   public void Scaling()
   {
       //Flip the scale direction when F key is pressed
       scaling.scaleUp = !scaling.scaleUp;

       //Stop old coroutine
       if (scaling.scaleCoroutine != null)
           StopCoroutine(scaling.scaleCoroutine);

       if (lightCoroutine != null)
           StopCoroutine(lightCoroutine);

       //Scale  up
       if (scaling.scaleUp)
       {
           //Start new coroutine and scale up within 5 seconds and return the coroutine reference
           rotating.rotateBack = false;
           scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration, playerCamera));
           if (dimlights.lightsOnOff == false)
               lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
       }

       //Scale Down
       else
       {
           //Start new coroutine and scale up within 5 seconds and return the coroutine reference
           rotating.rotateBack = true;
           scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.minSize, scaling.duration, playerCamera));
           if (dimlights.lightsOnOff == false)
               lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, scaling.duration)); ;
       }
   }
}

And in third script I want to call a method that will be in the ObjectsManipulation script maybe the same method Scaling maybe he will get a bool and if the bool is true make it scaling up/down automatic if it’s not true make it use a key.

This is the script for testing the Scaling :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScalingTest : MonoBehaviour
{
   ObjectsManipulation om;

   // Start is called before the first frame update
   void Start()
   {

   }

   // Update is called once per frame
   void Update()
   {
       om.Scaling();
   }
}

For example in the Update maybe to do : om.Scaling(false); for using the F key and om.Scaling(true); for automatic.

You didn’t mention what conditions should inform the object that it has grown large enough, and should start shrinking, or that it is small enough so it should start growing.

If you just want something that grows and shrinks forever, you can use Mathf.Sin(Time.timeSinceLevelLoad) as an input into your scaling function. That ranges from -1 to 1, so you might prefer to add 1 and divide by 2 to get 0 to 1 values.

Otherwise, you’ll need to specify what should cause the object to switch between growing and shrinking states.