Question by
ltrout1999 · Apr 27, 2016 at 01:30 AM ·
buttonholdpress
Button Press and Hold, instead of Click?
I am creating an arcade-tank game and the longer you hold the space bar (if you're using a PC/Mac), the more force will apply. I want to port over to mobile devices, so how can I set it to the longer the press and hold the button to apply more force?
Here is my code for the PlayerAttack:
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;
//public float MaxForce = 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;
}
}
Comment
Your answer
Follow this Question
Related Questions
Launch ball after too long waiting 1 Answer
How can i make a hold method for addforce? 0 Answers
how to hold button longer, the more powerful shot ? 0 Answers
How to get touch's positon when I hold the button?? 2 Answers
Need to press key 2 times to shoot 0 Answers