- Home /
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.
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)...
@Zedock, don't forget to click the check mark under the voting arrows if my answer helped you.
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.