【问题标题】:Unity Diagonal Movement Not Working统一对角线运动不起作用
【发布时间】:2016-04-14 14:51:34
【问题描述】:

我希望我的玩家沿对角线方向移动,这是我用来沿对角线向下移动的代码:

 if (Input.GetAxisRaw("Horizontal") > 0f && Input.GetAxisRaw("Vertical") < 0f)
 {
      front45 = true;
      rb.velocity = new Vector3(moveSpeed, -moveSpeed, 0f);
 }

然而,rigidbody2d 不会朝那个方向移动。它会上下左右移动,但不会对角线。

front45 = true 只是为了让动画师知道什么时候改变动画。

【问题讨论】:

  • 你的代码是否成功进入if语句? IE。是特定条件的问题,还是速度分配的问题?
  • 我尝试了列出的答案,似乎这成功了。因为我有这么多行代码来控制角色,可能是我哪里出错了

标签: unity3d unity5


【解决方案1】:

我会尝试这样的事情:

float h = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
float v = Input.GetAxisRaw("Vertical") * Time.deltaTime;

if (h != 0 && v != 0)
    front45 = true; //Not sure what this does, so I just left it inside the condition

rb.velocity = new Vector3(h * moveSpeed, v * moveSpeed, 0f);

这应该适用于任何方向。

【讨论】:

  • 这绝对是移动起来方便多了,也省了不少代码
  • @lain Blackwood 很高兴能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多