Easy way to multiply values in a vector?

Is there an easy/efficient way to multiply the values in a vector, or otherwise perform simple arithmetic to a vector?

Here’s the basic situation I’m in:
I’m using a vector I obtain from another function and applying it to my current function’s rigidbody’s velocity.
But I want to make some simple tweaks to those values; I want to multiply them by DeltaTime and another local variable (to control speed.)

Now if I was adjusting the values of the vector individually, it would simply be a matter of writing a line to multiply those values before they are assigned to the vector.
But since I’m retrieving the values of the vector from another function, I don’t have the same access to the variables that form the vector’s values.
Now I could create two new float variables, get their values from the vector, and then modify them, and then apply them back into the vector. But that just sounds terribly inefficient! That adds two extra variables to the memory and then an extra six cycles to the function’s execution. Isn’t there some way I can easily multiply all the values of a vector by a single value?

1 Like

Vector arithmetic defines scalar multiplication, as well as vector summation, dot product, and cross product.

Vector3 v = new Vector3(....); //get a vector
v *= 3f; //scales it by 3
v += someVector; //sums a vector
1 Like

This shows that when you take a vecter3 and multiply it by a number. it multiplies each of the vectors, .x , .y, .z by the number

        float speed = 5;
        rb = GetComponent<Rigidbody>();
        rb.AddForce(transform.forward);
        Debug.Log("force : " + rb.velocity);
        Debug.Log("+ speed : " + (rb.velocity * speed));

force : (0.0, -3.3, 0.0)

  • speed : (0.0, -16.7, 0.2)
    force : (0.0, -5.3, 0.3)
  • speed : (0.2, -26.5, 1.3)

Ahhh… I don’t know why I didn’t just try to actually multiply the vector instead of just assuming it wouldn’t work.
Did it used to not work that way? A lot of things are working easier than I expect and I swear things used to be different a few years ago.

is there a way to do this to individual axis

Vectors are structs or so-called “data types”. They live on the stack and propagate as concrete data, instead of being considered through a pointer to something lying on a heap.

That said, it is completely reasonable to extract their components on the fly, do whatever is needed, then reassemble the new thing back.

For example

var myVec = new Vector3(.127f, .148f, .459f);
myVec.Normalize();
myVec = new Vector3(3f * Mathf.Round(myVec.x), 0f, Mathf.Round(myVec.z));
Debug.Log(myVec.ToString());

In my practice I’ve discovered a few interesting patterns that can be used with vectors in order to minimize repetition and allow better chaining of commands, so there are ways and ways to streamline the common vector manipulation. But there is nothing particularly wrong with deconstructing them and then building new ones on the fly. It just looks ugly sometimes (and may be prone to errors).

For example this is the above code written my way (perhaps not the most useful example but still…)

myVec = myVec.normalized.x_z().Lambda( (i,v) => Mathf.Round(v) ).ScaledBy(x: 3f);

Edit:
If you’re interested here the extensions are defined as follows (you must make an extension class for this to work, because we can’t edit the actual Vector3 class)

public static Vector3 x_z(this Vector3 v) => new Vector3(v.x, 0f, v.z);

public static Vector3 Lambda(this Vector3 v, Func<int, float, float> lambda)
  => new Vector3(lambda(0, v.x), lambda(1, v.y), lambda(2, v.z));

public static Vector3 ScaledBy(this Vector3 v, float x = 1f, float y = 1f, float z = 1f)
  => new Vector3(x * v.x, y * v.y, z * v.z);

// aka component-wise multiplication for two vectors
public static Vector3 ScaledBy(this Vector3 v, Vector3 other)
  => new Vector3(other.x * v.x, other.y * v.y, other.z * v.z);

All in all, if you want to multiply X by 3 in a readable manner, you can do this

myVec = myVec.ScaledBy(x: 3f);

But nothing stops you from implementing ScaleX or something similar. Whatever the case, you can’t avoid having to access the individual component, multiplying it, and then reassembling all components into a new Vector3 to reflect this change. This is how structs work.

Edit2:
In fact, because structs in C# are confusing for beginners, let me clarify the above example of multiplying the whole thing by 3, because it’s actually coded like this under the hood

public static Vector3 operator *(float lhs, Vector3 rhs) => new Vector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
public static Vector3 operator *(Vector3 lhs, float rhs) => new Vector3(rhs * lhs.x, rhs * lhs.y, rhs * lhs.z);
1 Like