- Home /
How to lerp scale?
ok, so i am trying to lerp my scale, and stop when the scale size is half of what it was originally, the problem i have with the script i am about to show you is that it doesn't lerp, it just hops to the destination. using UnityEngine; using System.Collections;
public class testtrigger : MonoBehaviour {
bool yes = true;
int start = 1;
void Update ()
{
hippy ();
}
void hippy()
{
if (start == 1)
{
Vector3 size = transform.localScale;
Vector3 newSize = size / 2f;
float speed = 1f;
Debug.Log (yes.ToString ());
if (yes) {
Debug.Log (newSize.ToString () + "tostring");
Debug.Log (newSize.magnitude + "magnitude");
if (newSize + newSize == size) {
Debug.Log ("i did lerp");
transform.localScale = Vector3.Lerp (size, newSize, speed);
} else
Debug.Log ("else");
if (size - newSize == newSize) {
size = newSize;
transform.localScale = Vector3.Lerp (size, newSize, speed);
}
}
start --;
Debug.Log (start.ToString());
}
So i edited my script, and came up with another one. The problem with this script is it doesn't stop when it gets half it's size, it continues to decrease. This is because the variables keep changing each time in update, keeping the scale continuosly decreasing. Ive heard about implementing some sort of time constriction, but i don't know how to go about doing this. If anyone could help me i'd be extremely grateful.
using UnityEngine; using System.Collections;
public class testscript5 : MonoBehaviour {
float time = 2f;
// Update is called once per frame
void Update ()
{
Vector3 size = transform.localScale;
Vector3 newSize = size / 2f;
transform.localScale = Vector3.Lerp(size, newSize, time);
}
}
Also is there any possible way to 'lock' in the variables value, so that it can't ever increment or decrement away from it's original value, even if it was placed in update? And i know if i were able to spell out the x,y and z coordinates of the vector 3's i would be able to do it that way, but the thing is i plan on using this for collisions, and the vector3 variables are going to be collision info, so i'm not myself going to be setting the vector3's. If anyone would be able to help me i'd be extremely grateful.
Answer by Masterio · Jan 09, 2019 at 07:12 PM
using UnityEngine;
using System.Collections;
public class ScaleTest : MonoBehaviour
{
public Vector3 targetScale;
public float speed = 1f;
public Mode mode = Mode.Lerp;
public enum Mode
{
Lerp = 0,
MoveTowards = 1
}
void Update ()
{
if(mode == Mode.Lerp)
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime * speed);
else
transform.localScale = Vector3.MoveTowards(transform.localScale, targetScale, Time.deltaTime * speed);
if((transform.localScale - targetScale) == Vector3.zero)
enabled = false;
}
public void Run(Vector3 targetScale, Mode mode = Mode.Lerp)
{
this.targetScale = targetScale;
enabled = true;
}
}
Your answer
Follow this Question
Related Questions
Vector3.Lerp works outside of Update() 3 Answers
Object Scale Lerp with rigidbody addforce jump duration. 1 Answer
Making an object move smoothly without using Update 2 Answers
Saving the starting position of a Lerp during Update 2 Answers
Smoothly moving a 2D gameobject along a vector path 1 Answer