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