Unity - 帮助需要触摸并按住 UI 按钮来调用更新方法和自动触发(这里是脚本)
Unity - help needed to touch and hold UI button to call update method and auto fire (here is the script)
我是一个没有编码经验的新手,刚开始使用unity我正在关注brackeys Ray cast拍摄视频,https://youtu.be/THnivyG0Mvo,我创建了枪脚本但我想转它进入移动设备,所以我想通过按住 UI 按钮来自动射击。
plz,我需要帮助我知道没有代码。
这是我的代码。
我在想有没有办法用一些东西代替 input.getbutton 来制作一个在按住时自动触发的按钮。
谢谢,抱歉,如果这是一个愚蠢的问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
public void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
public void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit,
range))
{
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point,
Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
创建一个按钮,并向其添加一个新脚本。该脚本将负责处理按钮状态。基本上我们需要做的是..
当 OnPointerDown 发生时,设置一个值以指示按钮被按住。当 OnPointerUp 发生时,重置该值。
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickAndHoldButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler
{
public bool IsHeldDown => isHeldDown;
private bool isHeldDown;
public void OnPointerDown(PointerEventData eventData)
{
isHeldDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isHeldDown = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
}
}
然后在你的枪的更新中,你会检查按钮状态,看看它是否被按住。
public ClickAndHoldButton mobileFireButton;
void Update()
{
if (Time.time >= nextTimeToFire && (
Input.GetButton("Fire1") ||
mobileFireButton.IsHeldDown))
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
我是一个没有编码经验的新手,刚开始使用unity我正在关注brackeys Ray cast拍摄视频,https://youtu.be/THnivyG0Mvo,我创建了枪脚本但我想转它进入移动设备,所以我想通过按住 UI 按钮来自动射击。 plz,我需要帮助我知道没有代码。 这是我的代码。 我在想有没有办法用一些东西代替 input.getbutton 来制作一个在按住时自动触发的按钮。 谢谢,抱歉,如果这是一个愚蠢的问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
public void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
public void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit,
range))
{
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point,
Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
创建一个按钮,并向其添加一个新脚本。该脚本将负责处理按钮状态。基本上我们需要做的是.. 当 OnPointerDown 发生时,设置一个值以指示按钮被按住。当 OnPointerUp 发生时,重置该值。
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickAndHoldButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler
{
public bool IsHeldDown => isHeldDown;
private bool isHeldDown;
public void OnPointerDown(PointerEventData eventData)
{
isHeldDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isHeldDown = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
}
}
然后在你的枪的更新中,你会检查按钮状态,看看它是否被按住。
public ClickAndHoldButton mobileFireButton;
void Update()
{
if (Time.time >= nextTimeToFire && (
Input.GetButton("Fire1") ||
mobileFireButton.IsHeldDown))
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}