Hi everyone, i am working on Unity Tutorial Let’s Try RayCast Shooting, i have finished the script but when i click fire button, the console shows error LineRenderer.Setposition Index Out Of Bound and nothing happen, i have searching for a while but still no clue, this is urgent, please help me, Thank you.
Could you show us any of your code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour {
public int Damage=1;
public float FireRate = 0.25f;
public float WeaponRange=50f;
public float HitForce=100f;
public Transform GunEnd;
private Camera FpsCam;
private WaitForSeconds ShotDuration= new WaitForSeconds(0.07f);
private LineRenderer LaserLine;
private float NextFire;
void Start ()
{
LaserLine = GetComponent<LineRenderer>();
FpsCam = GetComponentInParent<Camera>();
}
void Update ()
{
if (Input.GetKey(KeyCode.Mouse0) && Time.time > NextFire)
{
NextFire = Time.time + FireRate;
StartCoroutine(ShotEffect());
Vector3 RayStartPoint = FpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit Hit;
LaserLine.SetPosition(0, GunEnd.position);
if(Physics.Raycast(RayStartPoint,FpsCam.transform.forward,out Hit,WeaponRange))
{
LaserLine.SetPosition(1,Hit.point);
}
else
{
LaserLine.SetPosition(1, RayStartPoint + (FpsCam.transform.forward * WeaponRange));
}
}
}
private IEnumerator ShotEffect()
{
LaserLine.enabled = true;
yield return ShotDuration;
LaserLine.enabled = false;
}
}
here is my Script
Um, hello, any suggestion?
Sorry, I was at home/in bed.
Take a look at Unity - Scripting API: LineRenderer.SetVertexCount
My guess is that you need to specify how many vertices your line will consist of, before setting their vertices.
Oh thank you, but LineRenderer.SetVertexCount is Obsolete and i can’t use that, it’s suggest me using startwidth in LineRenderer property and i’ve tried that too, but still error, can you please help me check the Unity Tutorial here
https://unity3d.com/learn/tutorials/lets-try/shooting-with-raycasts?playlist=41639
I did exactly the same in video but error
just forget it, i figure it out by setting the size in LineRenderer component to 2, thank you anyway
Sorry, I gave you an obsolete API. It should not be telling you to use startWidth instead, though, it should be telling you to use Unity - Scripting API: LineRenderer.numPositions
Taking a look at the tutorial now…
Ok, great ![]()