• 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 /
  • Help Room /
avatar image
0
Question by RoervigPruductins · Dec 11, 2017 at 12:33 PM · unity 5sizecubedecrease

How to make a cube shrink over time

I am very new to the game dev industry. And i dont know sh*t about coding. I want to make a (20, 1, 20,) Cube shrink over time. I have googled after some tutorials. But i couldn't find ANYTHING.... So i was hoping you could help me out.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by shadowpuppet · Dec 11, 2017 at 10:20 PM

This works on a simple cube I made. It decreases the size evenly ( x,y,z) .Change the growRate to a positive number to increase size. The lrger the digit ( whether positive or negative ) the faster it grows or shrinks

 using UnityEngine;
 using System.Collections;
 
 public class shrink : MonoBehaviour {
 
     public GameObject cube;
     public float growRate = -3f;
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         cube.transform.localScale  += new Vector3(0.1F, .1f, .1f) * growRate * Time.deltaTime;
     }
 }
 



   
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
avatar image
0

Answer by Legend_Bacon · Dec 12, 2017 at 09:25 AM

Hello there,

This should help you out:

 using System.Collections;
 using UnityEngine;
 
 public class Test : MonoBehaviour
 {
     private void Start()
     {
         //Call the function giving it a target scale (Vector3) and a duration (float).
         ScaleToTarget(new Vector3(10.0f, 2.5f, 7.5f), 2.5f);
     }
 
     public void ScaleToTarget(Vector3 targetScale, float duration)
     {
         StartCoroutine(ScaleToTargetCoroutine(targetScale, duration));
     }
 
     private IEnumerator ScaleToTargetCoroutine(Vector3 targetScale, float duration)
     {
         Vector3 startScale = transform.localScale;
         float timer = 0.0f;
 
         while(timer < duration)
         {
             timer += Time.deltaTime;
             float t = timer / duration;
             //smoother step algorithm
             t = t * t * t * (t * (6f * t - 15f) + 10f);
             transform.localScale = Vector3.Lerp(startScale, targetScale, t);
             yield return null;
         }
 
         yield return null;
     }
 }

Put that script on your cube, and it will shrink to target scale with a nice smoothing effect on start. It's up to you then to call ScaleToTarget() from other scripts whenever you require.

More about smoothing formulas HERE

Hope that helps!

~LegendBacon

Comment
Add comment · Show 5 · 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
avatar image RoervigPruductins · Dec 12, 2017 at 01:07 PM 0
Share

Hi, Whenever i put the script on my cube, It says.alt text

skærmbillede-2017-12-12-kl-140642.png (22.3 kB)
avatar image shadowpuppet RoervigPruductins · Dec 12, 2017 at 04:30 PM 0
Share

the actual name of the file must match the class. looks like you named the file of the script "size" but pasted in the code above which starts out " public class Test : $$anonymous$$onoBehaviour". either rename the script to Test or change the code to " public class size : $$anonymous$$onoBehaviour"

avatar image Legend_Bacon RoervigPruductins · Dec 13, 2017 at 09:05 AM 0
Share

Hello there

As ShadowPuppet said, in this case you need your class name to match the file's (script's) name. Either rename the file to "Test" (not recommended), or rename the class from "Test" to something else. In your case, I believe it would be size? I would recommend renaming it to something more intuitive, like "Scaler" or "Resizer".

Best of luck to you!

~LegendBacon

avatar image RoervigPruductins Legend_Bacon · Dec 17, 2017 at 04:27 PM 0
Share

Okay now it works. But it shrinks to fast, i would like it to be about 30 seconds to shrink. And then shrink in to (3, 1, 3,)

I am sorry if i seem to be ungrateful of your help and just seem like your were nothing, that is not what i mean. I my intension was to ask if you could help me something more, not to say that your job was not done well enough :).

EDIT: "It shrinks too fast."

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

145 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 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

Attemped to use GameObject.GetComponent().bounds.size.y to find the height of a sprite, but weird glitch happening when I do. 1 Answer

Dynamic UI image scale with Text length? 0 Answers

Decrease the pakage size of android game 1 Answer

Blend generated fbx import to unity problem 1 Answer

Can't put a text on an Object 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges