Android Unity控件

问题描述

我正在尝试将我的PC游戏代码统一转换为android,并且在控件更改上受阻。请帮忙!

这是代码:

获取火箭状态。

enum State { Dying,Alive,Transcending }
State state = State.Alive;

// Update is called once per frame

void Update()
{
    if (state == State.Alive)
    {
        RespondToThrustInput();
        RespondToRotateInput();
    }
}

当火箭与任何物体发生碰撞时,它会在将其状态从活着变为死亡之前检查其是否友好。

private void OnCollisionEnter(Collision collision)
{
    if (state != State.Alive) { return; }
    switch (collision.gameObject.tag)
    {
        case "Friendly":
            break;
        case "Finish":
            state = State.Transcending;
            audioSource.Stop();
            audioSource.PlayOneShot(finishgame);
            finishgameParticles.Play();
            Invoke("LoadNextScene",levelloaddelay);
            break;
        default:
            state = State.Dying;
            audioSource.Stop();
            audioSource.PlayOneShot(death);
            deathParticles.Play();
            Invoke("LoadFirstScene",levelloaddelay);
            break;
     }
}

private void LoadFirstScene()
{
       SceneManager.LoadScene(9);
}

使用构建索引加载下一个场景。

private void LoadNextScene()
{
    if (nextscenetoload > 7)
    {
        nextscenetoload = 0;
    }
    SceneManager.LoadScene(nextscenetoload);
}

用于点火或用力的空间以及用于播放音效的音频源。

private void RespondToThrustInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        ApplyThrust();
    }
    else
    {
        audioSource.Stop();
        mainengineParticles.Stop();
    }
}

施加推力是我根据火箭推力的逻辑编写的方法。

private void ApplyThrust()
{
    rigidbody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
    if (!audioSource.isPlaying)
        audioSource.PlayOneShot(mainengine);

    mainengineParticles.Play();
}

火箭旋转或左右旋转。在这里,我尝试使用A和D键旋转火箭

void RespondToRotateInput()
{
    float rotationThisFrame = rcsThrust * Time.deltaTime;
    if (Input.GetKey(KeyCode.A))
    {
        rigidbody.freezeRotation = true;
        transform.Rotate(Vector3.forward * rotationThisFrame);
        rigidbody.freezeRotation = false;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        rigidbody.freezeRotation = true;
        transform.Rotate(-Vector3.forward * rotationThisFrame);
        rigidbody.freezeRotation = false;
    }
}

解决方法

对于PC游戏,有一个键盘可用于控制,但对于android,有触摸,您必须验证屏幕上是否有触摸,例如:

nodemon src --watch * --exec \"electron .\"

您还需要更多的工作来确定触摸的位置并进行自定义...或者如果您对此输入处理不感兴趣,则可以使用Unity Assets Store中的一些资产来为您覆盖此部分

检查此链接以获取有关Unity文档中触摸控制的更多信息: https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...