【问题标题】:Controlling a player using touch input on the mobile device使用移动设备上的触摸输入控制播放器
【发布时间】:2015-06-21 09:46:34
【问题描述】:

我是 Unity 新手,正在学习滚动球的教程。
我要为移动设备和台式机创建它,它正在工作,但我唯一的问题是我无法创建触摸键箭头(左、右、上、下)来控制触摸屏设备上的播放器。

请检查我下面控制播放器的代码:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;

public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Texture2D button1; //button 1
public Texture2D button2; //button2
public Texture texture;

private Rigidbody rb;
private int count;
// Use this for initialization
void Start () {
    Screen.orientation = ScreenOrientation.LandscapeLeft;
    //GUITexture.texture = button1;
    rb = GetComponent<Rigidbody> ();
    count = 0;
    SetCountText ();
}

// Update is called once per frame
void FixedUpdate () {
    Screen.orientation = ScreenOrientation.LandscapeLeft;
    Screen.sleepTimeout = SleepTimeout.NeverSleep;
    if (SystemInfo.deviceType == DeviceType.Desktop) {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
        if (Input.GetKey("escape"))
        {
            Application.Quit();
        }

    }//END Desktop
    else 
    {
        float moveHorizontal = Input.acceleration.x;
        float moveVertical = Input.acceleration.y;
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit(); 
        }
        foreach(Touch touch in Input.touches)
        {
            //Always getting error here
            if (GUITexture.HitTest(touch.position) && touch.phase !=TouchPhase.Ended)
            {
                GUITexture.texture = button2;
                transform.Translate(Vector3.right*30*Time.smoothDeltaTime);
            }else if(GUITexture.HitTest(touch.position) && touch.phase !=TouchPhase.Ended)
            {
                GUITexture.texture = button1;
            }
        }
    }
    // Building of force vector 

}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag ("Pick Up"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText();
    }
}
void SetCountText()
{
    countText.text = "Count:" + count.ToString ();

}
}

【问题讨论】:

  • 你能告诉我错误是什么吗?
  • @MasihAkbari 错误错误 CS0120:需要对象引用才能访问非静态成员“UnityEngine.GUIElement.HitTest(UnityEngine.Vector3, UnityEngine.Camera)”

标签: android ios mobile unity3d touch


【解决方案1】:

您尝试像静态函数一样直接从GUITexture 调用HitTest,但HitTest 不是静态函数,您需要从GUITexture 类创建一个变量,然后从中调用HitTest 函数像这样的对象:

public GUITexture guiT;

if (guiT.HitTest(touch.position) && touch.phase !=TouchPhase.Ended)
{
    guiT.texture = button2;
    transform.Translate(Vector3.right*30*Time.smoothDeltaTime);
}
else if(guiT.HitTest(touch.position) && touch.phase !=TouchPhase.Ended)
{
    guiT.texture = button1;
}  

不要忘记将 guiT 变量分配给编辑器中的某些内容。

【讨论】:

  • 我认为我没有正确执行此操作,但我不确定在哪里。我已将按钮放到飞机上,以便我可以使用它来控制播放器,但它没有显示任何错误现在
猜你喜欢
  • 2020-11-22
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 2019-11-12
  • 2014-04-22
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多