- Home /
Help with casting and drawing multiple raycast along an angle
hey there,
Like the title says, i want to cast multiple Ray's along an angle. And draw them using a LineRenderer.
I hope someone can help me with this one, i find it pretty hard to come up with a solution. The script is meant for a weapon set in a top down shooter. I am trying to create a variety of weapons each with their own variables.
Some will have 1 bullet trace while others can spread 5 or more on a certain angle.
I tried it doing like this:
//this snippet is placed in an IEnumerator : Shoot(int amount, float spreadAngle, int count)
//the reason it is a IEnumerator is because some weapons will have a Burst fire type
//the AMOUNT variable is set for the amount of bullets fired
//SPREADANGLE variable is self explanatory, this will be the angle the bullets will be fired in
//COUNT variable is for the Burst function, 1 is just a regular weapon, 3 will have a burst of 3 bullets
float perRayAngle = spreadAngle / (amount - 1); //perRayAngle will evenly spread the Ray's along the angle
float startAngle = spreadAngle * -0.5f;
//I thought maybe using a List will help solve this, but it didn't
List<LineRenderer> lines = new List<LineRenderer>(amount);
for(int i = 0; i < amount; i++)
{
//couldn't check if the rayAngle was working, since when i pressed the Fire1 button it gave a null reference
Vector3 rayAngle = Quaternion.AngleAxis(startAngle+1*perRayAngle,fireTransform.forward) * fireTransform.forward;
//the Ray values
shootRay.origin = fireTransform.position;
shootRay.direction = rayAngle;
//bulletLine is a public LineRenderer, lines[i] will inherit its values
lines[i] = bulletLine;
//enable lines[i]
lines[i].enabled = true;
//set the lines[i] starting position to the fireTransform or "Muzzle" position
lines[i].SetPosition(0, fireTransform.position);
//for now i only draw the lines when it is hit, i will change this however
if(Physics.Raycast(shootRay, out shootHit, 100f))
{
lines[i].SetPosition(1, shootHit.point);
}
}
//firerate
nextFire = Time.time + fireRate;
I thought using a List will help this problem, since the amount of bullets fired change depending on the weapon.
However, it still didn't work. Whenever i press the "Fire1" key, i get a null reference.
So anyway, hope you can give me a few tips on this.
Cheers and thanks in advance, Darryl
Your answer
Follow this Question
Related Questions
Help with LineRenderer 1 Answer
A node in a childnode? 1 Answer
Hit Angle Of Ray to Surface? 1 Answer
hit point not working correctly . 1 Answer