使用guiTexture移动2D角色

时间:2014-09-03 00:01:17

标签: unity3d-2dtools

我有一个左移动的脚本,我想在Unity3D中使用我的精灵角色。 我想要它,这样每当按下guiTexture时,精灵就会移动。 这是该运动的脚本:

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);

}

1 个答案:

答案 0 :(得分:1)

将gui纹理传递给玩家脚本,并将其命名为#34; YourGuiTexture&#34;。

有各种逻辑检测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);
         }          
    }
}