用guiTexture移动2D角色

问题描述:

我有一个左移动脚本,我想在Unity3D中使用我的精灵角色。 我想要它,只要guiTexture被按下,精灵就会移动。 下面是运动的脚本:用guiTexture移动2D角色

public float maxSpeed = 10f; 
public GameObject player; 


void Start() {} 

void FixedUpdate() { 

    float move = Input.GetAxis ("Horizontal"); 

    if (move < 0) { 
     move = move; 
    } 

    rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 

} 

传给你的GUI纹理玩家脚本,并命名为“YourGuiTexture”。

有各种逻辑来检测GuiTexture打最简单的是下面:

在键盘:

public GUITexture YourGuiTexture; 

void Update() { 

    if (YourGuiTexture.HitTest(Input.mousePosition) //check if your mouse is on your gui texture 
    { 
     float move = Input.GetAxis ("Horizontal"); 

     if (move < 0) 

     { 
      move = move; 
     } 

     rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 
    } 

    } 

触摸移动设备:

public GUITexture YourGuiTexture; 

// Update is called once per frame 
void Update() 
{ 
    if (YourGuiTexture.HitTest(Input.GetTouch(0).position)) 
    { 

     if(Input.GetTouch(0).phase==TouchPhase.Began) 
     { 
      float move = Input.GetAxis ("Horizontal"); 

      if (move < 0) 

      { 
       move = move; 
      } 

      rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 
     }   
    } 
} 
+0

请问这仍然工作在移动设备上? – 2014-09-03 20:42:55

+0

实际上移动设备没有鼠标连接,其中大部分工作在触摸逻辑上我编辑了我的移动设备答案以及看看。 – 2014-09-04 06:25:29