0
votes

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;
  }
1
By the way, keep in mind not to set transform.position manually if you use rigid bodies and physics. In that case use the MovePosition function which ensures that the physics engine can immediately react correctly to the new position. Or even better: Use AddForce for smooth movement. Furthermore such things should be best placed in FixedUpdate() and not Update() because the former is in sync with the physics engine and the later is in sync with the screen refresh rate (which tends to fluctuate much more).AlexGeorg

1 Answers

2
votes

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