Held UI button C#
Hi, at start want to say sorry about my English. But i have a problem with shooting mechanics. I did control under the keyboard, but want to remake it fot sensor control. I have a mechanics with slider-charge shoot. When i held UI button it charge, but if i stop held it - it not shoot, it shoot only if LaunchForce = maxLaunchForce. If someone can help me, i ready to say thanks by sending couple $. Here's video how it work NOW, and THIS video how it should work. That's my code for shooting.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Shooting : MonoBehaviour
{
public int m_PlayerNumber = 1;
public Rigidbody m_Shell;
public Transform m_FireTransform;
public Transform FireHole2;
public Transform FireHole3;
public Slider m_AimSlider;
public float m_MinLaunchForce = 5f;
public float m_MaxLaunchForce = 15f;
public float m_MaxChargeTime = 0.75f;
private string m_FireButton;
public float m_CurrentLaunchForce;
private float m_ChargeSpeed;
private bool m_Fired;
public float fireRate = 2;
public float WaitTillNextFire;
private bool CorountineFireRate;
public bool TripleShoot = false;
private bool CorountineTriple;
public FixedButton FireButton;
IEnumerator FireRate(float time)
{
if (CorountineFireRate)
yield break;
CorountineFireRate = true;
yield return new WaitForSeconds(15);
fireRate = 2;
CorountineFireRate = false;
}
IEnumerator Triple(float time)
{
if (CorountineTriple)
yield break;
CorountineTriple = true;
yield return new WaitForSeconds(10);
TripleShoot = false;
CorountineTriple = false;
}
private void OnEnable()
{
m_CurrentLaunchForce = m_MinLaunchForce;
m_AimSlider.value = m_MinLaunchForce;
}
private void Start()
{
m_FireButton = "Fire" + m_PlayerNumber;
m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
}
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.gameObject.name == "FireRateUpgrade")
{
fireRate = 15;
Destroy(collisionInfo.gameObject);
StartCoroutine(FireRate(15));
}
if (collisionInfo.gameObject.name == "TripleShoot")
{
TripleShoot = true;
Destroy(collisionInfo.gameObject);
StartCoroutine(Triple(10));
}
}
private void Update()
{
WaitTillNextFire -= Time.deltaTime * fireRate;
if (WaitTillNextFire <= 0)
{
m_AimSlider.value = m_MinLaunchForce;
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
{
m_CurrentLaunchForce = m_MaxLaunchForce;
if (TripleShoot == false)
{
Fire();
}
else if (TripleShoot == true)
{
TripleFire();
}
}
else if (FireButton.Pressed == false)
{
m_Fired = false;
m_CurrentLaunchForce = m_MinLaunchForce;
}
else if (FireButton.pointerDown == true && !m_Fired)
{
m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
m_AimSlider.value = m_CurrentLaunchForce;
}
else if (FireButton.Pressed == true)
{
if (TripleShoot == false)
{
Fire();
}
else if (TripleShoot == true)
{
TripleFire();
}
}
}
}
void Fire()
{
m_Fired = true;
Rigidbody shellInstanse = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
shellInstanse.velocity = m_CurrentLaunchForce * m_FireTransform.forward;
m_CurrentLaunchForce = m_MinLaunchForce;
WaitTillNextFire = 1;
}
void TripleFire()
{
m_Fired = true;
Rigidbody shellInstanse = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
Rigidbody shellInstanse2 = Instantiate(m_Shell, FireHole2.position, FireHole2.rotation) as Rigidbody;
Rigidbody shellInstanse3 = Instantiate(m_Shell, FireHole3.position, FireHole3.rotation) as Rigidbody;
shellInstanse.velocity = m_CurrentLaunchForce * m_FireTransform.forward;
shellInstanse2.velocity = m_CurrentLaunchForce * FireHole2.forward;
shellInstanse3.velocity = m_CurrentLaunchForce * FireHole3.forward;
m_CurrentLaunchForce = m_MinLaunchForce;
WaitTillNextFire = 1;
}
}
That's my code for UI button with "hold" mechanics.
using UnityEngine.Events;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class FixedButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool Pressed;
public bool pointerDown;
private float pointerDownTimer;
public float requiredHoldTime;
public UnityEvent onLongClick;
public void OnPointerDown(PointerEventData eventData)
{
pointerDown = true;
Pressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
Reset();
Pressed = false;
}
void Update()
{
if (pointerDown)
{
pointerDownTimer += Time.deltaTime;
if (pointerDownTimer >= requiredHoldTime)
{
if (onLongClick != null)
onLongClick.Invoke();
Reset();
}
}
}
private void Reset()
{
pointerDown = false;
pointerDownTimer = 0;
}
}
Your answer
Follow this Question
Related Questions
I need help making a UI button that when pressed shoots a projectile. 0 Answers
EventTrigger OnPointerUp Slider Snap Back to Zero? 0 Answers
Can i update button listener? 0 Answers
disable UI button 4 Answers