• 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 FBanu · Sep 20, 2017 at 05:58 PM · 2dcollisiongameobjecttriggerscale

Trouble Descaling 2D object

Hi, I'm trying to make a game where one has to catch bars of soap. I want the player to catch the soap and hold it until it shrinks to a fraction of its original size.

This mechanic works for the most part, but once I reach a certain threshold, it shrinks down to the minimum size on its own, and I don't know why. Below is a video demonstrating the problem as well as the relevant code.

Video

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewSoap : MonoBehaviour {
 
     public float hp = 300;
     float totalHp;
     public float minHp;
     float scaler;
     private bool isCaught = false;
     Vector3 originalScale;
 
     // Use this for initialization
     void Start() {
         totalHp = hp;
         originalScale = new Vector3(GetComponent<Transform>().localScale.x, GetComponent<Transform>().localScale.y, GetComponent<Transform>().localScale.z);
 
     }
 
     // Update is called once per frame
     void Update() {
                                      
         print("health: " + hp + "|| scaler: " + scaler);
 
         if ((hp >= minHp) && isCaught)
         {
             hp--;
             scaler = hp / totalHp;
             GetComponent<Transform>().localScale = originalScale * scaler;  
         }
         else if( hp <= minHp)
         {
             // Destroy(gameObject);
         }
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
          if ((other.name == "Hand") && (other.GetComponent<Hand>().isOpen() == false))
          {
              isCaught = true;
              print("Caught!!");
          }
          else
          {
              isCaught = false;
          }
     }
 
     void onTriggerExit2D(Collider2D other)
     {
         if (other.name == "Hand")
         {
             isCaught = false;
         }
     }
 }


Any help would be greatly appreciated, thank you!!

Comment
Add comment · Show 1
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 hexagonius · Sep 20, 2017 at 08:40 PM 0
Share

It's OnTriggerExit2D, not onTriggerExit2D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheSOULDev · Sep 20, 2017 at 08:49 PM

You should use OnTriggerEnter instead of OnTriggerStay, use coroutines instead of Update, and then see if your bugged code is fixed. You can see that the way you constructed it doesn't work well as it is possible to set isCaught to true twice consecutively. Also since we don't have insight in the variables that actually matter, people probably can't tell you what other than the lousy implementation is wrong as the booleans that control the behaviour are not shown in the video. My guess is that because you implemented it in a way you did, your isCaught never set itself to false, as it seems to be the only way you would get the behaviour you're getting if there's no other script interfering.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Sphere and Cube collision won't work,Sphere vs Cube collision won't work 1 Answer

Find the highest point of a stack of gameobjects 2 Answers

2D Collision detection to load level. 1 Answer

Ignore a collider when there are multiple ones? 1 Answer

1 collider 2 fuctions, how? 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