Skip to main content
  1. Blogs/

Element-wise Vector Multiplication in Unity (Vector3.Scale)

·79 words·1 min·
unity code tips

I often need to multiply two vectors together one element at a time. This is called the Hadamard Product. Typically I do it this way:

Vector3 a=new Vector3(1,2,3);
Vector3 b=new Vector3(4,5,6);
Vector3 result = new Vector3(a.x*b.x, a.y*b.y, a.z*b.z);

// result: (4, 10, 18)

But there is a simpler way to do this.

Vector3.Scale(Vector3 a, Vector3 b) can perform this operation with just one command:

Vector3 a=new Vector3(1,2,3);
Vector3 b=new Vector3(4,5,6);
Vector3 result = Vector3.Scale(a, b);
//result: (4, 10, 18)

Related

The top 5 things Ive learned as an Indie Unity Developer
·1402 words·7 mins
unity programming code tips
Serializing Interfaces in Unity
·544 words·3 mins
unity programming
Unity Events
·279 words·2 mins
unity programming
Hiding the Screen Canvas in the Unity Editor
·214 words·2 mins
unity programming