I’m creating an arcade tank game and when you shoot you simply press and hold the space bar (on PCs/Macs, that is) to apply force; the longer you hold the space bar the more force, etc. I am wanting to port my game over to mobile devices and use a button. How can I set it up to the longer I hold a button, the more force it applies and to shoot when I let go of the button or it reaches max force? Also here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Shell;
using UnityEngine.EventSystems;
public class PlayerAttack : MonoBehaviour
{
public Rigidbody Shell;
public Transform FireTransform;
public Slider AimSlider;
public float MinForce = 15f;
public float MaxForce = 30f;
//publicfloatMaxForce=TankLevel.CurrentLevel*6f;
public float MaxCharge = 0.75f;
public bool Fired;
public Button BFire;
string FireButton;
float CurrentForce;
float ChargeSpeed;
void OnEnable ()
{
CurrentForce = MinForce;
AimSlider.value = MinForce;
}
void Start ()
{
FireButton = "Fire";
ChargeSpeed = (MaxForce - MinForce) / MaxCharge;
}
void Update ()
{
AimSlider.value = MinForce;
if (CurrentForce >= MaxForce && !Fired)
{
CurrentForce = MaxForce;
Fire ();
}
else if (Input.GetButtonDown (FireButton))
{
Fired = false;
CurrentForce = MinForce;
}
else if (Input.GetButton (FireButton) && !Fired)
{
CurrentForce += ChargeSpeed * Time.deltaTime;
AimSlider.value = CurrentForce;
}
else if (Input.GetButtonUp (FireButton) && !Fired)
{
Fire ();
}
}
public void Fire ()
{
Fired = true;
Rigidbody shellInstance = Instantiate (Shell, FireTransform.position, FireTransform.rotation) as Rigidbody;
shellInstance.velocity = CurrentForce * FireTransform.forward;
CurrentForce = MinForce;
}
}