• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by darryldonohoe · Nov 15, 2016 at 03:58 PM · raycastlistanglelinerendererhit

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

78 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with LineRenderer 1 Answer

Hit Angle Of Ray to Surface? 1 Answer

hit point not working correctly . 1 Answer

A node in a childnode? 1 Answer

Raycast to follow an object on one axis, and another on a different axis 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges