简体   繁体   English

统一对角运动不起作用

[英]Unity Diagonal Movement Not Working

I want my player to move in diagonal directions and this is the code I am using to move in a down diagonal way: 我希望播放器沿对角线方向移动,这是我用来沿对角线向下方向移动的代码:

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

however the rigidbody2d wont move in that direction. 但是,刚体2d不会朝该方向移动。 It will move up, down and from side to side, but never diagonal. 它会上下左右移动,但不会对角移动。

The front45 = true is just for the animator to know when to change animation. front45 = true只是让动画师知道何时更改动画。

I would try something like this: 我会尝试这样的事情:

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);

This should work on any direction. 这应该在任何方向上起作用。

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

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