Search code examples
c#unity-game-engineqvector3d

(Unity) How does vector multiplication and position updates work in unity?


This piece of code involves the multiplication of a Vector3 moveVector with a float moveSpeed and another float, Time.deltaTime. do these floats get multiplied to every value of the Vector3 (x, y, z)? Furthermore, if I write transform.position instead of GameObject.transform.position, am I right that the transform.position transforms the position of the global object, thereby updating the position of whatever GameObject/prefab this movement script is attached to?

void Move(Vector3 desiredDirection)
  {
   moveVector.Set(desiredDirection.x, 0f, desiredDirection.z);
   moveVector = moveVector * moveSpeed * Time.deltaTime;
   transform.position += moveVector;
  }

Solution

  • Yes. moveVector * moveSpeed * Time.deltaTime; takes each number from the vector and multiplies it with the move speed then again with Time.deltaTime.

    So if we have a vector 3, 2, 1 each axis is multiplied with the value: 3 * speed * deltaTme 2 * speed * deltaTme 1 * speed * deltaTime

    transform.position is the same as writing gameObject.transform.position. Because the script is attached to the gameObject.

    Notice the difference between the GameObject and gameObject.

    gameObject is the current object the script is attached to GameObject is the base class of the object