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 に答える