簡體   English   中英

我怎樣才能讓 pc unity 游戲也能在手機上運行?

[英]How can I make a pc unity game work on phone too?

我有一個 3d 塊滑梯游戲,你必須繞過障礙才能到達終點線。(非常基本)你按 A 向左,按 D 向右。 我也想為 android 手機制作游戲,所以我修改了一些代碼來實現這些:當我按下屏幕左側的任意位置到 go 左側時,就像按下按鈕一樣,右側部分也是如此。 (我不太擅長糾正代碼)而且它的工作方式非常奇怪,而不是它應該做的。

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

public class TouchControls : MonoBehaviour {


public float moveSpeed;
private object mousePos;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
    
}

 private void FixedUpdate()
{
    TouchMove();
}

void TouchMove()
{

        if(Input.GetMouseButton(0))
    {

        Vector3 mosuePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (mosuePos.x > 1)
        {

            transform.Translate(moveSpeed, 0, 0);

        }
        else if (mosuePos.x < -1)
        {
            transform.Translate(-moveSpeed, 0, 0);
        }
        }

}

}

您可以添加兩個面板,一個在左側,另一個在右側,並向它們添加按鈕行為,添加一個 function ,例如 MoveToRight() { transform.Translate(moveSpeed, 0, 0); } 並按下鼠標按鈕,當設置為 android 時,unity 將鼠標輸入作為手指輸入處理。

如果您需要觸控支持,您應該使用Touch ;)

void TouchMove()
{
    // If touch is not supported use the mouse as fallback
    if(Input.touchSupported)
    {
        if(Input.touchCount == 1)
        {
            var touch = Input.GetTouch(0);
            HandleTouch(touch.position);
        }
    }
    else
    {
        if(Input.GetMouseButton(0))
        {
            HandleTouch(Input.mousePosition);
        }
    }
}

void HandleTouch (Vector2 touchPosition)
{
    // Checks if the touch/click is on the right or left half of the screen
    if (touchPosition.x > Screen.width / 2f)
    {
        transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
    }
    else
    {
        transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM