使用C#中的操纵杆控制对象

使用C#中的操纵杆控制对象,c#,joystick,C#,Joystick,现在我正在使用WASD键来控制一个对象。现在我想用操纵杆来做同样的事情。有人能告诉我如何调整我的代码来做到这一点吗?我想做的就是将if(Input.getKey)空间替换为if(Input)空间,该空间将使用操纵手柄左右移动船 void drive() { //decrease speed if (Input.GetKeyDown(KeyCode.S)) { //in Drive if (acceleration > 0 &&

现在我正在使用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);
        }
    }
}
void驱动器()
{
//减速
if(Input.GetKeyDown(KeyCode.S))
{
//驾驶
如果(加速度>0&&
bucketMode==false)
{
加速度-=250;
}
//反过来
否则,如果(加速度<0&&
bucketMode==true)
{
加速度+=250;
}
}
//加速
if(Input.GetKeyDown(KeyCode.W))
{
//驾驶
如果(加速度<最大速度&&
bucketMode==false)
{
加速度+=250;
}
//反过来
否则,如果(加速度>分钟速度&&
bucketMode==true)
{
加速度-=250;
}
}
//停止船并切换铲斗模式
if(Input.GetKeyDown(KeyCode.Space))
{
加速度=0;
如果(bucketMode==false)
{
//倒转
bucketMode=true;
craft.bucket(1);//将bucket作为true发送
}
其他的
{
//开车
bucketMode=false;
craft.bucket(0);//将bucket作为false发送
}
}
//如果速度慢,则向前移动+
如果(加速度<0)
{
//以行驶速度的一半向后移动,运动平稳,速度取决于用户输入(加速度)
this.transform.Translate(Vector3.left*Time.smoothDeltaTime*(-acceleration/500)+1));
//更新正在显示的飞行器速度
工艺.变速率((加速度/500)*1.94);
}
//如果速度较低,则向后移动-
否则,如果(加速度>0)
{
//以平滑B运动向前移动,速度取决于用户输入(加速度)
this.transform.Translate(Vector3.right*Time.smoothDeltaTime*((加速度/250)+1));
//更新正在显示的飞行器速度
工艺.变速率((加速度/250)*1.94);
}
//如果速度为0,则静止不动
其他的
{           
}
//右转
if(Input.GetKey(KeyCode.D))
{
//随着速度的增加,使船平稳地向左转弯,转弯角度也随之增加
这个.transform.Rotate(Vector3.forward*Time.smoothDeltaTime*25/*((2*加速度/250)+1)*/);
//更新船舶信息(航向和速度)
工艺变更标题(1*Time.smoothDeltaTime*25/*((2*加速度/250)+1)*/);
工艺.变速率((加速度/250)*1.94);
//若船并没有向前移动,那个么就缓慢地向前移动,这样船就不会在原地转弯
如果(加速度==0)
{
this.transform.Translate(Vector3.right*Time.smoothDeltaTime/2);
}
}
//左转
if(Input.GetKey(KeyCode.A))
{
//随着速度的增加,船平稳地向右转弯,转弯角度也随之增加
这个.transform.Rotate(Vector3.back*Time.smoothDeltaTime*25/*((2*加速度/250)+1)*/);
//更新船舶信息(航向和速度)
工艺变更标题(-1*Time.smoothDeltaTime*25/*((2*acceleration/250)+1)*/);
工艺.变速率((加速度/250)*1.94);
//若船并没有向前移动,那个么就缓慢地向前移动,这样船就不会在原地转弯
如果(加速度==0)
{
this.transform.Translate(Vector3.right*Time.smoothDeltaTime/2);
}
}
}

您有多个选项可供选择

  • 使用

  • 您可以使用这些库中的任何一个来获取输入。很抱歉,但这并不像更改if语句那么简单。

    代码中最重要的部分是A和D的if语句,以及我刚才介绍的所有内容,以便您可以看到船是如何移动的,那么其中一个看起来如何?我所看到的所有操纵杆代码都解释了如何实例化操纵杆,但没有解释如何使用操纵杆进行速度或方向控制。@Flotolk首先,你需要了解这些代码,看看你想使用哪一个操纵杆,然后我还提供了链接,让你通过工作示例更仔细地查看。