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
0
Question by Xysch · Nov 12, 2014 at 09:45 PM · c#gameobjecttransformlerpshrinking

Shrinking an Object

I'm trying to shrink something when it comes in contact with another object and I have no idea why it isn't working. If you could help me it would be appreciated. Thanks! Here's the code that should shrink the game object:

     public float targetScale = 0.1f;
     public float shrinkSpeed = 2f;
 
             gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
 



Comment
Add comment · Show 3
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 ExtremePowers · Nov 12, 2014 at 09:56 PM 0
Share

What is the problem, doesn't it shrink at all, or does it go too fast?

avatar image Linus · Nov 12, 2014 at 09:58 PM 0
Share

Please post the full code

avatar image Xysch · Nov 12, 2014 at 10:02 PM 0
Share

It does not shrink at all and here's the full code:

 public class $$anonymous$$agnetism : $$anonymous$$onoBehaviour {
     
     public GameObject planet;
     public float gravityFactor = 0.5f;
     private SpriteRenderer spriteRenderer;
     private Color start;
     private Color end;
     private float t = 0.0f;
     private bool go = true;
     public float targetScale = 0.1f;
     public float shrinkSpeed = 2f;
 
     void Start () {
         spriteRenderer = GetComponent<SpriteRenderer> ();
         start = spriteRenderer.color;
         end = new Color (start.r, start.g, start.b, 0.0f);
     }
 
     void FixedUpdate(){
         if (Input.Get$$anonymous$$ouseButton (0)) {
             rigidbody2D.AddForce ((planet.transform.position - transform.position).normalized * rigidbody2D.mass * gravityFactor / (planet.transform.position - transform.position).sqr$$anonymous$$agnitude);
         }
     }
     
     void OnTriggerEnter2D(Collider2D coll){
         if (coll.gameObject.tag == "Player") {
             StartCoroutine(fade ());
         }
     }
 
     IEnumerator fade(){
         while (go == true){
             gameObject.transform.localScale = Vector3.Lerp(gameObject.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
             t += Time.deltaTime;
             renderer.material.color = Color.Lerp (start, end, t / 1f);
             if (renderer.material.color.a <= 0.0)
                 Destroy (gameObject);
                 yield return null;
         }
     }
 }

1 Reply

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

Answer by ExtremePowers · Nov 12, 2014 at 10:02 PM

Try this:

 private float curScale;
 public float startScale;
 public float shrinkSpeed;
 
 void Update() {
     curScale = Mathf.MoveTowards(curScale, startScale, Time.deltaTime * shrinkSpeed);
     gameObject.transform.localScale = new Vector3(curScale, curScale, curScale);
 }
  
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 Xysch · Nov 12, 2014 at 10:08 PM 0
Share

With that bit of code it doesn't do anything at all anymore.

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

27 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

Related Questions

Rotating a Vector3 in Instantiation 1 Answer

CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer

How would you create a vertical endless runner with lerping barriers where the player translates on oblique vectors in C#? 0 Answers

Why isn't LateUpdate detecting changes from Update? 1 Answer

Simultaneously moving GameObjects using C# script. 3 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