简体   繁体   English

(Unity) 向量乘法和位置更新如何统一工作?

[英](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.这段代码涉及将 Vector3 moveVector 与浮点 moveSpeed 和另一个浮点 Time.deltaTime 相乘。 do these floats get multiplied to every value of the Vector3 (x, y, z)?这些浮点数是否乘以 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?此外,如果我编写transform.position而不是GameObject.transform.position,我是否正确地认为transform.position会转换全局对象的位置,从而更新此移动脚本附加到的任何游戏对象/预制件的位置?

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

Yes.是的。 moveVector * moveSpeed * Time.deltaTime; takes each number from the vector and multiplies it with the move speed then again with Time.deltaTime.从向量中获取每个数字并将其与移动速度相乘,然后再与 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所以如果我们有一个向量 3, 2, 1 每个轴都乘以这个值:3 * speed * deltaTme 2 * speed * deltaTme 1 * speed * deltaTime

transform.position is the same as writing gameObject.transform.position . transform.position与编写gameObject.transform.position相同。 Because the script is attached to the gameObject.因为脚本是附加到游戏对象上的。

Notice the difference between the GameObject and gameObject.注意 GameObject 和 gameObject 之间的区别。

gameObject is the current object the script is attached to GameObject is the base class of the object gameObject 是脚本附加到的当前对象 GameObject 是对象的基类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM