【问题标题】:How to get touch joystick in unity如何统一触摸操纵杆
【发布时间】:2020-12-22 14:47:24
【问题描述】:

我想知道如何将此键盘输入更改为移动操纵杆,这是我将使用的代码。我还希望用新的操纵杆来实现新的输入系统。我使用 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),
        0.0f,
        Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    );
    rb.rotation = Quaternion.Euler(
        0.0f,
        0.0f,
        rb.velocity.x * -tilt
    );
}

【问题讨论】:

  • 我没有通读所有内容,但它看起来像this discussion,它附带的视频可能会回答你的问题。

标签: ios unity3d touch joystick


【解决方案1】:

我得到了我的解决方案。使用Unity的摇杆包,改两行代码

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

到此使用操纵杆

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

不要忘记添加操纵杆变量。

public Joystick joystick;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多