public Vector3 (float value) constructor

It’s not a major thing and I can live fine without it, but it’d be nice if there was a Vector3 constructor that took in 1 float and set its x, y, and z value to that float.

This would mainly be useful for matters relating to scale. I.e.:

transform.localScale = new Vector3 (size);

Anyways, just a little tidbit that should be added if you guys have some spare time.

1 Like
transform.localScale = Vector3.one * size;

It’s not the same thing.

You could do an extension method…

public static void SetScale(this Transform t, float val)
{
    t.localScale = new Vector3(val, val, val);
}

And then just:

transform.SetScale(5.0f);

But yes, that only handles a specific case. The idea is still valid :slight_smile:

1 Like

That works :smile:.

There are many solutions - all equally simple. new Vector3 (val) just satisfies more cases and performs the best.

1 Like

It looks like its not something you can do your self

http://stackoverflow.com/questions/4782034/is-it-possible-to-create-constructor-extension-method-how

I personally would be against the idea of a single float constructor. It would likely cause me hidden errors in my code if I accidental passed in a single float. This is not as far fetched as it sounds, some of the constructors I use for Vector3 have brackets all over the show.

You could also use a static method…and disguise it to appear as a Vector3 constructor.

public static class V
{
    public static Vector3 ector3(float value)
    {
        return new Vector3(value, value, value);
    }
}

void Start()
{
    Vector3 v = V.ector3(1);
}

Yep, nothing out of the ordinary here, move along folks.

But really, Vector3 already has a two parameter constructor which sets the z to zero, so I suspect if it ever got a 1 parameter constructor it would just set X and Y to zero to keep with the paradigm.

1 Like

I’ve coded worse. This made it into production once. Caused several hours of head scratching when a future dev changed the implementation of DoSomething and everything just started randomly disappearing with no error messages.

try {
    gameObject.GetComponent<SomeClass>().DoSomething();
} catch {
    Destroy(gameObject);
}
1 Like

Honestly curious. Is this just your preference or are there arguments of performance/common coding practice?

I don’t favor either style, so I wonder.

Vector3.one * size expands to something like this…

var one = new Vector3 (1f,1f,1f);
return new Vector3 (one.x * size, one.y * size, one.z * size);

All that for initializing a V3 with components set as ‘size’. There’s unnecessary mental overhead, not to mention performance.

Trust me. The performance effects of scaling the transform will outweigh the cost of multiplying a vector.

But if that is to expensive just inline it. As in:

new Vector3 (val, val, val);
1 Like

“not to mention performance”

It’s a very simple task. I was actually wrong in discounting Nanity’s solution because there’s not many ways one can go wrong with it. new Vector3 (val, val, val) is a bit impractical when val is Agent.SelectionRadiusVisual though.

1 Like
int val = some.complex.Bit().of.Code;
new Vector3 (val, val, val);

As floats are a struct there is essentially no performance cost of doing this. And if any of the chain operations are expensive this will work faster then calling the operations three times.

As floats are a struct there is essentially no performance cost of doing this. And if any of the chain operations are expensive this will work faster then calling the operations three times.[/QUOTE]
I referring to typing Agent.SelectionRadiusVisual 3 times. I could care less about the performance.

Regardless, float isn’t a normal struct - it’s a primitive and gets inlined.

Considering it on its own it’s a fine suggestion. Considering it in the context of the existing constructors, it’s inconsistent and thus potentially confusing. (Ideally everyone would read the docs and it wouldn’t be an issue. But that’s hardly a realistic expectation…)

The constructor Vector3(float, float, float) sets the x, y and z components of a vector according to the parameters.
The constructor Vector3(float, float) sets the x and y components according to the parameters and defaults the z component to 0.
With that in mind, it’s quite reasonable to expect a constructor Vector3(float) to follow suit, setting the x component as per the parameter and defaulting both y and z to zero.

Your requested behaviour is also quite reasonable, though. There can only be one… so which one should it be?

It could be that this is a deliberate omission. By providing neither they’re forcing people to do these (very simple!) operations themselves, which has the advantage of ensuring that we actually get what we wanted, as opposed to leaving it as an opportunity for subtle and easy to miss mistakes.

Edit: For what it’s worth, I use the “Vector3.one * number” approach. This makes it clear that I explicitly want to use uniform scaling, as opposed to the three axes just happening to have the same values.

3 Likes

Ah, I see the ambiguity now. The problem could be mitigated with a descriptive parameter name but the pattern would still be inconsistent with the other constructors.

1 Like

Vector3 is already overloaded, eg the implicit Vector2 conversions are a major pain. You should prefere descriptive static constructor functions. This way the reader of zour code knows what your code means without jumping around in your code.
eg. Vector3.xxx(float x) which fills the first parameter into all channels, analogous to the swizzling in shaders. Could be called Vector3.FromFloat or Vector3.Fill too.