1

我有一个左移动脚本,我想在 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);

}
4

1 回答 1

1

将您的 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);
         }          
    }
}
于 2014-09-03T06:47:04.277 回答