0
votes

I have a script which fires a linerenderer from a start to end position with each click. I want to have a linerender that is constant until the mouse button is released. Like a power charge. What do I need to add in the script to make this happen?

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class 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

1 Answers

0
votes

Well you can start by use Input.GetButtonUp to stop the fire. That way you can start on Input.GetButtonDown and stop only when buttonUp. If you need to execute code every frame you can also use Input.GetButton.

That's the basic idea.