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.
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.
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.
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.
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.
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.
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.