【问题标题】:Use Joysticks in C# to control objects在 C# 中使用操纵杆来控制对象
【发布时间】:2013-07-29 15:50:35
【问题描述】:

现在我使用 WASD 键来控制一个对象。现在我想用操纵杆来做同样的事情。谁能告诉我如何调整我的代码来做到这一点?我要做的就是将 if (Input.getKey) 空间替换为 if (Input) 空间,该空间将使用操纵杆左右移动船。

void drive()
{
    //decrease speed
    if (Input.GetKeyDown(KeyCode.S))
    {
        //in Drive
        if (acceleration > 0 && 
            bucketMode == false)
        {
            acceleration-= 250;
        }
        //in Reverse
        else if (acceleration < 0 &&
            bucketMode == true)
        {
            acceleration += 250;
        }
    }

   //Increases Speed 
   if (Input.GetKeyDown(KeyCode.W))
    {
        //in Drive
        if (acceleration < maxSpeed &&
            bucketMode == false)
        {
            acceleration+= 250;
        }
        //in Reverse
        else if (acceleration > minSpeed &&
            bucketMode == true)
        {
            acceleration -= 250;
        }
    }

    //stops the boat and toggles Bucket mode
    if (Input.GetKeyDown(KeyCode.Space))
    {
        acceleration = 0;
        if (bucketMode == false)
        {
            //goes into revers
            bucketMode = true;
            craft.bucket(1); //sends bucket as true
        }
        else
        {
            //goes into drive
            bucketMode = false;
            craft.bucket(0);  //sends bucket as false
        }
    }
    //moves forward if speed is +
    if (acceleration < 0)
    {
        //moves backwards at half the speed than in drive, in a smooth motion, speed depending on user input(acceleration)
        this.transform.Translate(Vector3.left * Time.smoothDeltaTime * ((-acceleration / 500) +1));

        //updates craft speed being displayed
        craft.ChangeSpeed((acceleration/500) * 1.94);
    }
    //moves backward if speed is -
    else if (acceleration > 0)
    {
        //moves forward in a smoothb motion, speed depending on user input(acceleration)
        this.transform.Translate(Vector3.right * Time.smoothDeltaTime * ((acceleration / 250) + 1));

        //updates craft speed being displayed
        craft.ChangeSpeed((acceleration/250) * 1.94);
    }
    //stands still if speed is 0
    else
    {           
    }

    //turns right
    if (Input.GetKey(KeyCode.D))
    {
        //turns the boat to the left smoothly, as speed increases, so does turn angle
        this.transform.Rotate(Vector3.forward * Time.smoothDeltaTime * 25 /* ((2 * acceleration / 250) +1)*/);

        //updates boat information(heading and speed)
        craft.ChangeHeading(1 * Time.smoothDeltaTime * 25 /* ((2 * acceleration / 250) +1)*/);
        craft.ChangeSpeed((acceleration/250) * 1.94);

        //if boat is not moving forwards, moves forward slowly, so boat doesn't turn in place
        if (acceleration == 0)
        {
            this.transform.Translate(Vector3.right * Time.smoothDeltaTime / 2);
        }
    }
    //turns left
    if (Input.GetKey(KeyCode.A))
    {
        //turns the boat to the right smoothly, as speed increases, so does turn angle
        this.transform.Rotate(Vector3.back * Time.smoothDeltaTime * 25 /* ((2 * acceleration / 250) +1)*/);

        //updates boat information(heading and speed)
        craft.ChangeHeading(-1 * Time.smoothDeltaTime * 25 /* ((2 * acceleration / 250) +1)*/);
        craft.ChangeSpeed((acceleration/250) * 1.94);

        //if boat is not moving forwards, moves forward slowly, so boat doesn't turn in place
        if (acceleration == 0)
        {
            this.transform.Translate(Vector3.right * Time.smoothDeltaTime / 2);
        }
    }
}

【问题讨论】:

  • 代码中最重要的部分是 A 和 D 的 if 语句,我刚刚包括了所有其他内容,以便您可以看到船是如何移动的

标签: c# joystick


【解决方案1】:

这里有多种选择

  1. 使用SlimDx
  2. SharpDx
  3. DirectX

您可以使用这些库中的任何一个并获取输入。对不起,但不是换个if语句那么简单。

【讨论】:

  • 好的,那么其中一个看起来如何?我见过的所有操纵杆代码都说明了如何实例化它们,但没有说明如何将它们用于速度或方向控制。
  • @Flotolk 首先,您需要查看这些内容并查看要使用哪个,然后我还提供了链接供您仔细查看工作示例。
猜你喜欢
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多