I’m having trouble with the syntax of using operators with these 2.
Can I just get a super simple snippet showing the syntax from someone please (Vector3 operator * for instance). I know I’m just missing something dumb ![]()
I’m having trouble with the syntax of using operators with these 2.
Can I just get a super simple snippet showing the syntax from someone please (Vector3 operator * for instance). I know I’m just missing something dumb ![]()
I am not sure if I get what your asking, so if this answer is out to lunch forgive it. ![]()
The Vector3 operator info is just to let you know what operators are overloaded. so the Vector3 operator * just means you can multiply a Vector3 with what they list (floats and vectors) and it will automatically do the proper Vector multiplication with each Vector element. So you don’t have to break up the vector to apply the multiplication to each element (x,y,z). So the operators just tell you what the built-in operator functions do and what you can do them with.
Hope that is what you wanted.
-Jeremy
Oh, this also means that to multiply a vector by another vector you just:
myVector * myOtherVector;
//or as we see in the docs:
myFloat * myVector;
-Jeremy
No, I get an error saying “Operator ‘*’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘UnityEngine.Vector3’” which you say is exactly what the operator should be doing:
Ha!!! As I’m writing this I just realized I need to use Vector3.Cross… I think? Still confused maybe :roll:
Weird…I will try…I know adding them this way works fine.
-Jeremy
Weird, I get operator cannot be applied to operands of type UniteEngine.Vector3 in C#. The + and - work though. Docs outdated?
You could do: x * other.x, y * other.y, z * other.z yourself in a function if you really want to. ![]()
That’s weird because it’s in the docs and that’s how oveloaded operators should work.
-Jeremy.
Yeah, + and such works, and (V.x * Y.x, V.y * Y.y, V.z * Y.z) works fine, I’m just trying to learn more elegant syntax I guess. Thanks for giving it a go!
No problem. I am pretty sure this is not intended behaviour or documented correctly, or you and I are just thick.
Be good to know as it doesn’t make sense for us to write our own math code for stuff like this.
-Jeremy
Well my thickness is undisputed, but I’m searching through every example script I have and I can’t find a single use of this function so maybe we’re both missing something?
You can’t multiply two vectors together. You can only multiply them with floats (=scaling a vector) or by Quaternions (= rotaion and scaling of a vector)
If you read the documentation for Vector3.operator *, you’ll see that there’s no Vector3 operator * (Vector3 left, Vector3 right) Either left or rigth side must be a float.
Ok. Would scale work to achieve the same result? Didn’t notice it before.
For my own knowledge, why can’t the * operator be overloaded to multiply two vectors? I know operator overloading is kind of limiting in C# but I don’t know about C++. You don’t have to answer that one, just curious. ![]()
-Jeremy
Yeah, I was starting to feel that this was the case… Jeremy and I both hallucintaed the Vector3 * Vector3 documentation I guess. But why isn’t there a method to do this… if I can do (V.x * Y.x, V.y * Y.y, V.z * Y.z) then why not turn it into a single method like Cross?
Go easy on my programming stupidity ![]()
BTW antenna, I don’t hink cross is what you want. Cross doesn’t just multiply the vectors, it bacisally returns a vector perpendicuar to the two sent if I remember correctly. It looks to me like scale is what you want.
And I am guessing here with my limited knowledge, but I looked around and couldn’t find ANY code example of overloading the * for two vectors. Not in any engine math functions, not anything. I think it must have something to do with types or something? I know with C# operator overloading at least one of the variables you send it has to be of a user defined type or class which is weird and limiting. But I don’t know about C++
-Jeremy
Because there is no single mathematical definition on what multiplying two vectors means. So instead we defined the different multiplication-like operations as Vector3.Cross for the cross product and Vector3.Dot for the dot product and Vector3.Scale for the component-wise scaling operation.
Yeah, sync1b explained this to me on the IRC tonight. Scale is the trick for what I need to do (I’ll try it in the morning), I don’t know why I missed this in the docs.
Thanks guys.
I missed both scale and the fact that there was no operator defined for two vectors. :shock: A sign I need to get to bed. lol. Thanks freyr.
-Jeremy