Search Unity

Feature Request Add multiply operator into Vector structs.

Discussion in 'Scripting' started by zezba9000, Aug 21, 2022.

  1. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    Unity needs to add multiply operator into its Vector structs.
    It would be so nice if Unity would update its math library to support multiplication so I don't have to use a static method just to short hand the code.

    upload_2022-8-20_19-8-7.png
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    The Vector3.Scale() function already exists.

    https://docs.unity3d.com/ScriptReference/Vector3.Scale.html

    I think the reason that they chose this name is to avoid confusing vector multiplication with "cross product" and "dot product" which are the first thing that mathematicians would think when you say you want to multiply vectors.
     
    zezba9000, Anthiese and Bunny83 like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    As halley alluded to, there's actually at least three different operations that one might reasonably consider "multiplication" of two Vectors:
    • Dot product
    • Cross product
    • hadamard product / aka Vector3.Scale
    Having one of these three get to use the * operator would probably just lead to more confusion.
     
    Anthiese and Bunny83 like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    Yes, that's usually the reason behind this decision. Though if you want a math library that is closer to the usual CG \ hlsl shader syntax you can use unity's mathematics package and the types that come with it. Like float3.

    This library was designed with high compatibility between shader code and C# code and to be burst compiler friendly. It does support the component wise (hadamard) product as the default multiplication.

    However since it's a separate library, there is some casting required between the mathematics types and the build in types like Vector3
     
  5. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    Using the '*' operator in matrix, quat or vector for complex multiplication is actually the incorrect place to handle these special ops & out of line with other principles in modern lang design.

    This is a software lang not a mathematical one from 2000 BC. Its tools to describe meaning aren't limited to short hands to save paper.

    Regardless I'll stick to Vector3.Scale in Unity.