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 Zedock · Jun 28, 2015 at 01:07 PM · unity 5gameobjectscalesizeif-statements

What is the difference between these, and how do I fix it.

 using UnityEngine;
 using System.Collections;
 
 public class ExplosionPhysics : MonoBehaviour {
 
     public Vector3 Increment;
     public Vector3 maxSize;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         transform.localScale += Increment * Time.deltaTime;
 
         if(transform.localScale == maxSize)
         {
             Destroy(this.gameObject);
         }
     }
 }

And,

 using UnityEngine;
 using System.Collections;
 
 public class ExplosionPhysics : MonoBehaviour {
 
     public Vector3 Increment;
     public Vector3 maxSize;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         transform.localScale += Increment * Time.deltaTime;
 
         if(transform.localScale >= maxSize)
         {
             Destroy(this.gameObject);
         }
     }
 }
 

The part in question is the:

            if(transform.localScale == maxSize)
            {
             Destroy(this.gameObject);
         }

And,

             if(transform.localScale >= maxSize)
             {
                    Destroy(this.gameObject);
                }
 

maxSize is equal to 15 on the x, y and z.

I need to have the transform.localScale >= maxSize however the only thing that will work is to have it == maxSize. However it skips over 15 exactly, probably because Time.deltaTime is a float. How can I rewrite this code so that I can check if the current scale is >= to another scale.

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
2

Answer by Dave-Carlile · Jun 28, 2015 at 01:16 PM

Vector3 has an equality operator which returns true of the vectors are equal. However, comparing floats (the Vector3 x, y, and z values are float) generally doesn't work as expected due to floating point precision and rounding.

There is no "greater than or equal to" operator for Vectors in Unity, probably because it wouldn't be clear exactly what you're comparing. In your case it sounds like you're trying to compare magnitudes (vector lengths), which you can do like this...

 if (transform.localScale.magnitude >= maxSize.magnitude)...

Comment
Add comment · Show 2 · 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 Zedock · Jun 28, 2015 at 10:22 PM 0
Share

Thanks a lot

avatar image Dave-Carlile · Jun 28, 2015 at 11:41 PM 1
Share

@Zedock, don't forget to click the check mark under the voting arrows if my answer helped you.

avatar image
0

Answer by DiegoSLTS · Jun 28, 2015 at 01:51 PM

If you know everything will be scaled proportionally on every axis then compare just one of them, like:

 if(transform.localScale.x >= maxSize.x)

If x, y and z scales can be different and you're definition of "greater than" is that every axis should be bigger then check all of them one by one, like:

 if(transform.localScale.x >= maxSize.x && transform.localScale.y >= maxSize.y && transform.localScale.z >= maxSize.z)

There's no concept of "greater than" for vectors.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Match a GameObject's size to another's? 2 Answers

Problem with shadows close up (Peter Panning) 0 Answers

How do you change size of an object by its units rather than scale? 1 Answer

actual size of an object 2 Answers

Positioning gameobjects by a corner, not by center 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