简体   繁体   English

Unity 中的无人机 Controller:统一输入系统的运动问题

[英]Drone Controller in Unity: movement issues with the unity inputSystem

I'm trying to build a controller for a unity gameobject to move it similary to a normel quadrotor drone.我正在尝试为统一游戏对象构建 controller 以将其移动到类似于正常四旋翼无人机的移动。 So the drone should be able to ascend and descend by mobing the left stick up and down, turn around by moving the left stick sidewards and move horizontally by using the right stick.所以无人机应该可以通过上下左右移动左摇杆进行升降,通过左右移动左摇杆来回旋,通过使用右摇杆进行水平移动。

I tried implementing it with the unity inputSystem, but unfortunately, it doesn't really work the way it is supposed to.我尝试使用统一输入系统来实现它,但不幸的是,它并没有真正按照预期的方式工作。 Movements are mostly not smooth and the rotation causes the horizonzal movement to move in wrong directions.运动大多不平滑,旋转会导致水平运动向错误的方向移动。

Here is my code:这是我的代码:

    public void OnMove(InputValue inputValue)
    {
        //horizontal movement
        Debug.Log(inputValue.Get());
        Vector3 move = new   Vector3(inputValue.Get<Vector2>().x * 10 * Time.deltaTime, 0, inputValue.Get<Vector2>().y * 1 * Time.deltaTime);
        move = Quaternion.Euler(0, rotation, 0) * move;
        //playerDrone.transform.position += new Vector3(inputValue.Get<Vector2>().x * 10 * Time.deltaTime, 0, inputValue.Get<Vector2>().y * 10 * Time.deltaTime);
        playerDrone.transform.Translate(move, Space.World);
    }

    public void OnClockwiseRotation()
    {
        //rotation of drone clockwise
        playerCam.transform.Rotate(0, 0.5f, 0, Space.World);
        rotation += 0.5f;
    }

    public void OnCounterclockwiseRotation()
    {
        //rotation of drone counterclockwise
        Debug.Log("Rotation");
        playerCam.transform.Rotate(0, -0.5f, 0, Space.World);
        rotation += 0.5f;
    }

    public void OnAscend1()
    {
        //ascend drone
        playerDrone.transform.position += new Vector3(0, 0.1f, 0);
        Debug.Log("Ascend");
    }

    public void OnDescend()
    {
        //descend drone
        Debug.Log("Descend");
        playerDrone.transform.position += new Vector3(0, -0.1f, 0);
    }

Does anyone know why the movement is so problematic with that implementation?有谁知道为什么该运动的实施如此成问题?

Thanks in advance提前致谢

There are a few mistakes有几个错误

  • Your rotation and your ascending are frame-rate dependent.您的旋转和上升取决于帧速率。 Here you didn't use Time.deltaTime在这里你没有使用Time.deltaTime

  • In both OnClockwiseRotation and OnCounterclockwiseRotation you doOnClockwiseRotationOnCounterclockwiseRotation你都做

    rotation += 0.5f;
  • You are using once playerDrone and another time playerCam .. you should probably stick to one as it sounds like you are only rotating a camera but not the drone.您正在使用一次playerDrone和另一次playerCam .. 您可能应该坚持使用一个,因为听起来您只是在旋转相机而不是无人机。

  • In general I would not hardcode the values into your code but rather expose some class fields so you can adjust the velocities vis the Inspector without having to recompile一般来说,我不会将这些值硬编码到您的代码中,而是公开一些 class 字段,以便您可以通过检查器调整速度而无需重新编译

You probably would rather do something like你可能更愿意做类似的事情

// Adjust these in the Inspector!

// in angle per second
public float rotationSpeed = 45;

// in meter per second
public float ascendingSpeed = 1;
public float forwardSpeed = 1;
public float sidewardsSpeed = 1;

public void OnMove(InputValue inputValue)
{
    //horizontal movement
    var input = inputValue.Get();
    Debug.Log(input);
    var move = new Vector3(input.x * sidewardsSpeed * Time.deltaTime, 0, input.y * forwardSpeed * Time.deltaTime);
    move = Quaternion.Euler(0, rotation, 0) * move;
    playerDrone.transform.Translate(move, Space.World);
}

public void OnClockwiseRotation()
{
    //rotation of drone clockwise
    var angle = rotationSpeed * Time.deltaTime;
    playerDrone.transform.Rotate(0, angle, 0, Space.World);
    rotation += angle;
}

public void OnCounterclockwiseRotation()
{
    //rotation of drone counterclockwise
    Debug.Log("Rotation");
    var angle = rotationSpeed * Time.deltaTime;
    playerDrone.transform.Rotate(0, -angle, 0, Space.World);
    rotation -= angle;
}

public void OnAscend1()
{
    //ascend drone
    playerDrone.transform.position += Vector3.up * ascendingSpeed * Time.deltaTime;
    Debug.Log("Ascend");
}

Thanks for the answer The hints helped to make it a lot smoother.感谢您的回答提示有助于使其更加顺畅。 Also, I noticed that using the velocity of a rigidbody instead of the absolute position also improves the performance of the drone.另外,我注意到使用刚体的速度而不是绝对的 position 也可以提高无人机的性能。

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

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