我有一个脚本,每次单击都会从开始位置到结束位置触发一个线性渲染器。我想拥有一个在释放鼠标按钮之前保持不变的划线器。就像充电一样。我需要在脚本中添加些什么才能实现此目的?
using System.Collections;
using 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;
}
}
答案 0 :(得分:0)
好吧,您可以使用Input.GetButtonUp
来阻止火灾。
这样,您可以从Input.GetButtonDown
开始,仅在buttonUp时停止。
如果您需要每帧执行代码,也可以使用Input.GetButton
。
这是基本思想。