- Home /
Use the line renderer for simulate shotgun shoot
Hello, i'm trying to use the line renderer for draw various lines from shotgun to a end point, for this i use a for loop with a Raycast2D, the Raycast works fine, but i only see a one line renderer when i shoot, this is the code what i using:
if (weapon == Weapon.Shotgun) {
for (int i = 0; i < 4; i++) {
RaycastHit2D shotgunHit;
Quaternion spreadAngle = Quaternion.Euler (0.0f, 0.0f, 4f * i);
shotgunHit = Physics2D.Raycast (firePosition, spreadAngle * rayDir, distance, whatToHit);
if (Input.GetButtonDown ("Fire1") && fireRate == 0.0f) {
line.enabled = true;
line.SetPosition (0, firePoint.position);
if (shotgunHit) {
if (shotgunHit.collider.tag == "Enemy") {
line.SetPosition (1, shotgunHit.point);
}
} else {
line.SetPosition (1, firePoint.position + (spreadAngle * rayDir * distance));
}
}
The objective is draw a 4 line renderer between the shotgun and the end of the raycast using the for loop.
Regards and thanks.
Answer by azatoch · Feb 07, 2017 at 05:48 PM
I think line renderer can only draw a single line with different positions, so to solve my problem I created a GameObject containing a line renderer, assigning the start and end of the line renderer in a script, then instantiate this GameObject using the for-loop to create the required GameObjects.
Your answer
Follow this Question
Related Questions
Rigidbody2D.AddRelativeForce not working 1 Answer
Enemy Line of Sight Help Needed 2 Answers
Shoot in proper direction with spread? 1 Answer
how to make a bullet spread ( shotgun ) in unity 2D 3 Answers