如何统一获得触摸操纵杆

问题描述

我想知道如何将此键盘输入更改为移动游戏杆,这是我将要使用的代码。我还希望使用新的操纵杆来实现新的输入系统。我使用 Unity 和 C#。

public float fireRate = 0.5F;
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
private float myTime = 0.0F;
private float nextFire = 0.5F;
private Rigidbody rb;
private AudioSource audioSource;

void Start()
{
    rb = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
}

void Update()
{
    myTime = myTime + Time.deltaTime;

    if (Input.GetButton("Fire1") && myTime > nextFire) {
        nextFire = myTime + fireRate;
        Instantiate(shot,shotSpawn.position,shotSpawn.rotation);

        audioSource.Play();

        nextFire = nextFire - myTime;
        myTime = 0.0F;
    }
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    
    Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    rb.veLocity = movement * speed;

    rb.position = new Vector3(
        Mathf.Clamp(rb.position.x,boundary.xMin,boundary.xMax),Mathf.Clamp(rb.position.z,boundary.zMin,boundary.zMax)
    );
    rb.rotation = Quaternion.Euler(
        0.0f,rb.veLocity.x * -tilt
    );
}

解决方法

我得到了我的解决方案。使用 unity 中的操纵杆包,更改 2 行代码`在此处输入代码

float moveHorizo​​ntal = Input.GetAxis("Horizo​​ntal"); float moveVertical = Input.GetAxis("Vertical");enter code here

到这个与操纵杆一起工作

enter code float moveHorizontal = joystick.Horizontal;
float moveVertical = joystick.Vertical;

在此处输入代码

不要忘记添加操纵杆变量。 enter code here公共操纵杆操纵杆;