Quantcast
Channel: Questions in topic: "hold"
Viewing all articles
Browse latest Browse all 154

Button Press and Hold, instead of Click?

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

Viewing all articles
Browse latest Browse all 154

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>