按住鼠标按钮时进行充电
Posted
技术标签:
【中文标题】按住鼠标按钮时进行充电【英文标题】:Power charge shot while holding mousebuttondown 【发布时间】:2019-09-25 06:18:31 【问题描述】:我有一个脚本,每次点击都会从头到尾触发一个 linerenderer。我想要一个在释放鼠标按钮之前保持不变的衬里渲染器。就像充电一样。我需要在脚本中添加什么来实现这一点?
using System.Collections;
使用 System.Collections.Generic; 使用 UnityEngine;
公共类 RayCastShot : MonoBehaviour
public float fireRate = 0.25f;
public float weaponRange = 50f;
public float hitForce = 100f;
public Transform gunEndLeft;
public Transform gunEndRight;
private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private LineRenderer lineRenderer;
private float nextFire;
public Material mat1;
public Material mat2;
void Start()
lineRenderer = GetComponent<LineRenderer>();
fpsCam = GetComponent<Camera>();
void Update()
if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
nextFire = Time.time + fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
lineRenderer.SetPosition(0, gunEndLeft.position);
lineRenderer.material = mat1;
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
lineRenderer.SetPosition(1, hit.point);
//get reference to hit point
if(hit.rigidbody !=null)
hit.rigidbody.AddForce(-hit.normal * hitForce);
else
lineRenderer.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
if (Input.GetButtonDown("Fire2") && Time.time > nextFire)
nextFire = Time.time + fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
lineRenderer.SetPosition(0, gunEndRight.position);
lineRenderer.material = mat2;
//lineRenderer.material = new Material(Shader.Find("Particles/Priority Additive"));
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
lineRenderer.SetPosition(1, hit.point);
//get reference to hit point
if (hit.rigidbody != null)
hit.rigidbody.AddForce(-hit.normal * hitForce);
else
lineRenderer.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
private IEnumerator ShotEffect()
lineRenderer.enabled = true;
yield return shotDuration;
lineRenderer.enabled = false;
【问题讨论】:
【参考方案1】:您可以先使用Input.GetButtonUp
来阻止火灾。
这样你就可以从Input.GetButtonDown
开始,只有在 buttonUp 时才停止。
如果需要每帧执行代码,也可以使用Input.GetButton
。
这是基本的想法。
【讨论】:
以上是关于按住鼠标按钮时进行充电的主要内容,如果未能解决你的问题,请参考以下文章