Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jenniferwalsh · Jan 09, 2019 at 06:45 PM · vector3updatescalelerponcollisionenter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges