Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by yosoycody · May 06, 2013 at 12:45 AM · c#transformscale

Animate the scaling of a gameObject

How can I animate the scaling of a gameObject over time? Basically, when the image gets collided into, I want it to scale bigger temporarily, and then back to its original size. I'm using the following code to make it scale bigger, which works:

 snareImage= GameObject.FindWithTag("snareImage");
 snareImage.transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);

But how can I animate this and scale back to it's original size?

Thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · May 06, 2013 at 01:10 AM

Use Vector3.Lerp; it's easiest if you use a coroutine. You can work off of this; it's basically the same thing as the Translation function except you'd use transform.localScale instead of transform.position.

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
8

Answer by goryfigment · May 01, 2016 at 05:24 PM

I just want to add this answer for non-coders and maybe an even better way of animating the scaling of an object. This can be done using Animation tool Unity provides.

  1. First, Go to to Window -> Animation so you are viewing the animation tab.

  2. Highlight the GameObject in the Scene or any Prefab you want to create an animation for.

  3. A Create Animation button will appear in the Animation tab. Click it.

  4. Now you are prompt to create an animation. On the left corner of it there should be a red dot or record button. Click on it.

  5. Now that you are recording anything you do will be recorded. Now drag the RED line to a preferred time.

  6. Scale the gameobject under the INSPECTOR tab to the preferred scale.

  7. You will now see Keys be created under the RED line. Congratulations you have now created your animation!

  8. Now all you have to do is play the animation when the necessary through code or you can add to your Created Animation to your controller and add parameters to play the animation when necessary!

I know this can be hard to follow for some people. So here I will provide a video that closely demonstrates of what I am talking about: https://www.youtube.com/watch?v=dfsl3S7Mx_M

Comment
Add comment · Show 1 · 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 error17191 · May 21, 2017 at 10:44 PM 0
Share

Do you think that this might have any bad impact on performance?, Does using coroutines for such situations have better performance than this?

avatar image
0

Answer by gharbill · May 06, 2013 at 02:30 AM

You have to put it inside Update() function Update() function runs every frame so in each frame you object will scale a delta-scale amount.

See the docs for how `Update()` function works

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 martipello · Feb 18, 2016 at 05:42 PM

yes i know this is an old post but i was tring to do the same i came up with the following script, but im a noob really so this can be expanded upon using lerp and such but i didnt need it hope somebody finds a use for this

    using UnityEngine;
    using System.Collections;

   public class bounce : MonoBehaviour {

 public AudioClip bounceNoise;
 public AudioSource source;        //Set in Inspector 
 public float speed;                           //Set in Inspector 
 public GameObject bouncer;              //Set in Inspector 
 public float explosionStrength = 100;    //Set in Inspector 

 void Awake() {
     source = GetComponent<AudioSource>();
 }
 void Update()
 {
     
 }
 IEnumerator MyCoroutine()
 {
     scale();
     yield return new WaitForSeconds(speed);
     normalFlash();
 }
 void OnCollisionEnter(Collision _other)
 {
     _other.rigidbody.AddExplosionForce(explosionStrength, this.transform.position, 5);
     source.PlayOneShot(bounceNoise, 1f);
     StartCoroutine(MyCoroutine());
 }
 void scale()
 {
     bouncer.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
 }
 void normalFlash()
 {
     bouncer.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
 }
 }

`

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

18 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

Related Questions

C# Need Help Refining Ricochet Script 1 Answer

C# Smoothing Out transform.Translate 4 Answers

scalefactor issue 0 Answers

C# How to Drag and Scale with Mouse Window 0 Answers

TextMeshPro Height 0 Answers


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