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 alpayiba · May 21, 2017 at 03:08 AM · c#2dtransformvector3.lerp

Changing size of a 2d object

I have a 2d rectangle. It will be 10 units long in the beginning, then it will be scaled to x percent of the 10 units, then 10 units again. But it wont directly change size, we will see it getting longer and shorter. When i tap it will stay at the size it is when tapping happens.

I have written and deleted code many times. Used coroutines, vector3.lerp etc. but nothing has worked.

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
0

Answer by Vandarthul · May 21, 2017 at 09:14 AM

Here is a simple script that does what you want by using both Lerp and SmoothDamp.

 using UnityEngine;
 
 public class SmoothDampTest : MonoBehaviour
 {
 
     private Transform myTransform;
     private Vector3 velocity;
 
     public bool useLerp;
     public Vector3 target;
     public float smoothTime;
     public float lerpSpeed;
     // Use this for initialization
     void Start()
     {
         myTransform = transform;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (useLerp)
         {
             myTransform.localScale = Vector3.Lerp(myTransform.localScale, target, Time.deltaTime * lerpSpeed);
         }
         else
         {
             myTransform.localScale = Vector3.SmoothDamp(myTransform.localScale, target, ref velocity, smoothTime);
         }
 
     }
 }

Attach it to a game object and try both Lerp and SmoothDamp to see if it's what you want. To use lerp:

  • Select the object that you've attached the script and set these variables on editor:

  • Use Lerp : true

  • Target : Set some target( note that if any of this value is 0, object might not be visible)

  • Lerp Speed: Some value(greater the value, faster the transaction)

To use SmoothDamp:

  • Use Lerp : false

  • Target : Set some target(note that if any of this value is 0, object might not be visible)

  • Smooth Time: Some value(lesser the value, faster the transaction, 0 is instant)

You can change the values in runtime by the way. This is to understand the behaviour and usage of lerp and smoothdamp. You need to prepare your own implementation(setting a percentage value with your start X value, in your case it's 10). Also please refer to this link to see the correct usage and details of Lerp. Lerp is not meant to produce a smoothing effect since it is a linear function but passing Time.deltaTime produces faster-start slower-end like effect since the t value is a percentage to target value.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

how to make the player stay within screen in a simple 2d game. C# 5 Answers

Random Y Axis Direction On Prefab Within Boundaries (C#) 0 Answers

Sprite is not shown moving 1 Answer

Emerging Gap when moving my "Snake" 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