问题描述
我正在尝试使用此脚本向前移动统一游戏对象,但一直遇到错误 CS0019:运算符 * 无法应用于类型为.Vector3"和Vector3"的操作数.为什么这是我不明白.
Hi i am trying to move a unity gameobject forward with this script but keep running into a Error CS0019: Operator * cannot be applied to operands of types '.Vector3' and 'Vector3'. why is this I dont understand it.
我的代码是:
transform.Translate (Vector3.forward * Time.deltaTime * (transform.localScale * 05f));
推荐答案
根据this,Vector3有一个operator*的重载,它的另一个参数是一个浮点数(即只能将一个Vector3乘以一个标量)该操作返回另一个Vector3.
According to this, Vector3 has one overload for operator*, which takes a float as the other parameter (I.e., you can only multiply a Vector3 by a scalar) The operation returns another Vector3.
Transform.localScale
也返回一个 Vector3,再乘以 0.5f 返回一个 Vector3.这意味着您的代码正在尝试将两个 Vector3 相乘,但它无法做到.
Transform.localScale
also returns a Vector3, and multiplying it by 0.5f again returns a Vector3. Which means your code is attempting to multiply two Vector3s, which it can't do.
也许你想要:
transform.Translate (Vector3.forward.Scale(transform.localScale * 0.5f) * Time.deltaTime);
这篇关于错误 CS0019:运算符 * 不能应用于“Vector3"和“Vector3"类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!