D
D
Demigodd2017-01-23 15:50:26
Unity
Demigodd, 2017-01-23 15:50:26

How to make an object zoom smoothly?

How to make smooth zoom? for example, an object with a size of 1,1,1 touched the trigger and its value changed to 2,2,2. But smoothly from 1,1,1 to 1.2,1.2,1.2 then 1.3 and so on up to 2.
Actually there is an increase code, but it instantly increases from 1 to 2.

using UnityEngine;
using System.Collections;

public class Eat : MonoBehaviour
{
  public string Tag;
  public float Increase;

  void OnTriggerEnter(Collider other)
  {
    if(other.gameObject.tag == Tag)
    {
      transform.localScale += new Vector3(Increase, Increase, Increase);  // ?
      Destroy(other.gameObject);
    }
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2017-01-23
@Demigodd

Lerp to Update by time. You remember the current scale and the target one, set the time for how much you need to change, and each frame is interpolated between them.
Or use plugins like tweener.

O
Oleg Pogrebnyak, 2017-01-23
@S0HardCore

Option with coroutines. Launch via:

IEnumerator SlowScale()
{
   for (float q = 1f; q < 2f; q += .1f;)
   {
      transform.localScale = new Vector3(q, q, q);
      yield return new WaitForSeconds(.05f)
   }
}

Where .1f is the size change amount and .05f is the change rate.

A
Asin Capper, 2019-07-18
@fpetrov2016

new Vector3(Increase, Increase, Increase) * Time.deltaTime;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question