2015-06-21 91 views
1

我是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(); 

} 
} 
+0

你能告诉我什么是错误? –

+0

@MasihAkbari ERROR rror CS0120:需要一个对象引用来访问非静态成员'UnityEngine.GUIElement.HitTest(UnityEngine.Vector3,UnityEngine.Camera)' –

回答

0

你试图直接从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变量some​​th来自编辑。

+0

我想我没有做到这一点,但我不知道在哪里。我已将按钮放入飞机,以便我可以使用它来控制播放器,但现在不会显示任何错误 –

相关问题