【问题标题】:Ping pong between 2 points fails两点之间的乒乓球失败
【发布时间】:2016-11-27 20:15:04
【问题描述】:

我正在尝试在 unity3d 中的 2 点之间移动一个对象,似乎这个主题有很多答案,但是当我尝试解决这个问题时,我总是遇到一个我不知道如何解决的错误,所以实际上我试着这样做:

void Update () {
    transform.position = Vector3(Mathf.PingPong(Time.time,10.0f), transform.position.y, transform.position.z);
}

我收到此错误:

Assets/PingPong.cs(7,38):错误 CS0119:表达式表示 type,其中应为 variablevaluemethod group

我做错了什么?我是初学者,需要一些帮助:/

【问题讨论】:

  • 你忘记Vector3之前的new关键字了吗?

标签: c# unity3d unity5


【解决方案1】:

要创建具有 x、y 和 z 值的 Vector3,您必须使用 new 关键字。

transform.position = new Vector3(Mathf.PingPong(Time.time, 10.0f), transform.position.y, transform.position.z);

一个例外是使用静态Vector3 函数返回预定义的Vector3 值,例如Vector3.zeroVector3.back 和其他值。

Vector3struct 数据类型。您可以了解为什么在创建新 Vector3(struct)here 时需要使用 new 关键字。

【讨论】:

  • 将修复错误,但不解释或回答问题。
  • @devRicher 我只是放了一个链接,因为之前已经回答了这部分问题。
  • 我得到了答案,而且效果还不错,但是如何提高速度并控制起点和终点?
  • 您可以创建一个控制速度的浮点变量,然后将其乘以Mathf.PingPong 值。比如float speed = 100那么你可以做transform.position = new Vector3(Mathf.PingPong(Time.time, 10.0f) * speed, transform.position.y, transform.position.z);
  • 嗯,非常感谢你,那太好了,我该如何控制起点和终点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多