Unity 2D - 如何配置游戏杆?

时间:2017-01-13 15:50:27

标签: c# unity3d 2d joystick xbox360

在查找各种教程和指南而不是修复我的解决方案之后,我想知道如何在Xbox 360控制器上设置左侧控制器,以便控制我的角色。我已尝试在InputManager中设置我的水平轴和垂直轴,但尽管如此,每当我运行游戏时,角色都会静止不动,操纵杆不起作用。我知道控制器在测试ABXY按钮后工作。任何帮助,将不胜感激。谢谢!

注意:在InputManager中,我将类型设置为操纵杆轴,将轴设置为相应的方向(水平,x轴:垂直,y轴),并将joy数设置为“从所有方向获取运动”操纵杆”。

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testScript : MonoBehaviour {

public GameObject circle;
public float range;

// Use this for initialization
void Start () {
    circle = GameObject.Find ("Circle");
}

// Update is called once per frame
void Update () {
    float translation = Time.deltaTime * 10;
    bool xbox_a = Input.GetButton ("XboxA");
    bool xbox_b = Input.GetButton ("XboxB");
    bool xbox_x = Input.GetButton ("XboxX");
    bool xbox_y = Input.GetButton ("XboxY");
    float Hvalue = Input.GetAxis ("Horizontal");
    float Vvalue = Input.GetAxis ("Vertical");

    float xPos = Hvalue * range;
    float yPos = Vvalue * range;

    circle.transform.Translate(xPos, yPos, 0);

    /*if (Input.GetKey ("w")) {
        circle.transform.position += Vector3.up * translation;
    } if (Input.GetKey ("a")) {
        circle.transform.position += Vector3.left * translation;
    } if (Input.GetKey ("s")) {
        circle.transform.position += Vector3.down * translation;
    } if (Input.GetKey ("d")) {
        circle.transform.position += Vector3.right * translation;
    }*/
    if (Input.GetKey ("e") || xbox_y) {
        circle.GetComponent<Renderer>().material.color = Color.yellow;
    }
    if (Input.GetKey ("r") || xbox_x) {
        circle.GetComponent<Renderer>().material.color = Color.blue;
    }
    if (Input.GetKey ("f") || xbox_b) {
        circle.GetComponent<Renderer>().material.color = Color.red;
    }
    if (Input.GetKey ("c") || xbox_a) {
        circle.GetComponent<Renderer>().material.color = Color.green;
    }

}
}

0 个答案:

没有答案