0
\$\begingroup\$

I added new buttons in my game made with unity 5.0 and the new UI feature.

The player keeps on moving when button is clicked instead of moving just once.

In the normal controls I make it move only once by changing back the x and y of movement to 0.

moving.x = moving.y = 0;

But how will I do that for button function?

I am calling each left, right, up, down , gun function separately in each button:

using UnityEngine;
using System.Collections;

public class UI : MonoBehaviour {

    public Vector2 moving = new Vector2();
    public int Bulletlimit = 0;
    public int MaxBulletlimit = 3;
    public bool Gun;
    private float lastShotTime ;
    public float fireDelay = 0.2f;
    public Transform BulletDirection;

    public Bullet bullet;

    // Use this for initialization
    void Start () {
        lastShotTime = Time.time;
    }

    // Update is called once per frame
    void Update () {

        moving.x = moving.y = 0;
        if (Input.GetKey ("right")) {
            right ();
        } else if (Input.GetKey ("left")) {
            left ();
        }

        if (Input.GetKey ("up")) {
            up();
        } else if (Input.GetKey ("down")) {
            down ();
        }


        if (Input.GetKey ("s")) {
            gun ();
        }   

    }

    public void BulletCount()
    {
        Bulletlimit = Bulletlimit - 1;
    }

    public void left()
    {
        moving.x = -1;
    }

    public void right()
    {
        moving.x = 1;
    }

    public void up()
    {
        moving.y = 1;
    }

    public void down()
    {
        moving.y = -1;
    }

    public void gun()
    {
        if(Gun){
            if(Bulletlimit < MaxBulletlimit)
            {
                if(Time.time > lastShotTime + fireDelay)
                {
                    Bullet clone = Instantiate (bullet, BulletDirection.position, Quaternion.identity) as Bullet;
                    Bulletlimit = Bulletlimit + 1;
                    lastShotTime = Time.time;
                }
            }
        }

    }

}
\$\endgroup\$
3
  • \$\begingroup\$ So are you saying that the player moves continuously every frame, but you want to move your player just once? \$\endgroup\$
    – aslg
    Commented Jun 9, 2015 at 0:50
  • \$\begingroup\$ yes only when the button is pressed as soon as its released player should stop moving \$\endgroup\$ Commented Jun 9, 2015 at 2:12
  • \$\begingroup\$ but currently once i click the button, player keeps on moving with out stopping \$\endgroup\$ Commented Jun 9, 2015 at 2:13

3 Answers 3

1
\$\begingroup\$

try using Input.GetKeyDown instead of Input.GetKey

\$\endgroup\$
2
  • \$\begingroup\$ Input.GetKey i have used for keyboard inputs which are working correctly, but i was using the left() and right() in the buttons which doesn't work, the UI takes care of the onlick event itself \$\endgroup\$ Commented Jun 8, 2015 at 8:33
  • \$\begingroup\$ i am using the new UI of unity 5.0 to make the button directly instead of GUI \$\endgroup\$ Commented Jun 8, 2015 at 8:53
1
\$\begingroup\$

Add EventTrigger to your UI BUttons ,particularly the OnPointerUp which you can use for moving.x=moving.y=0.Everytime the button is released your values will be reset.

\$\endgroup\$
0
\$\begingroup\$

You are using the proper callback; Input.GetKey is the "held down" callback (doesnt reset each frame).

I believe your problem is coming from the setting of x and y to 0 at the start of your update.

Instead you should only set them to 0 as else{} blocks for your key tests.

\$\endgroup\$
2
  • \$\begingroup\$ Thats what's happening implicitly. If he doesnt update the moving values by the conditons then they will be zero, that is if none of the keys were pressed. \$\endgroup\$
    – aslg
    Commented Jun 9, 2015 at 0:54
  • \$\begingroup\$ i suspect that's not the case here. since he's setting to zero before the test for key press, he's changing the value regardless if the key is still held down. I suspect this is causing a race condition between the rendering and update threads. \$\endgroup\$
    – Stephan
    Commented Jun 23, 2015 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .