Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 RogueDeus · Oct 19, 2011 at 08:11 PM · anglelinerendererdirectional

Help with Directional 2D Cone

I am prototyping a first attempt at a top down game and hope someone might point me to some code examples.

I have figured out how to create a simple 2D cone shape, but I am not sure how to center it in a particular direction. The cone will eventually indicate the direction, and margin of error for a ranged attack, centered in the opposite direction of another position set behind the cones origin. Much like lining up a telescope.

Of course, I have to start by simply being able to direct the cone to begin with... and so far, everything I have tried has failed pretty badly. I do not have a very strong grasp of trig, but I learn fast.

Here is my code: It will draw a simple cone using LineRenderer quite well.

 private void DrawLineCone(Transform origin, float angle, float radius)
 {
     int points = 3;
     Range = radius;

     float radian = angle * Mathf.Deg2Rad;
         //Debug.Log("Rad:\t" + radian + " Angle:\t" + (Mathf.Rad2Deg * radian) );
     float radianFract = radian / (float)points;
         //Debug.Log("Fract:\t" + radianFract);
     Vector3 center = origin.transform.position;    //Where unit stands
     
     lineRenderer.SetVertexCount(points + 3);    //Add start/finish points

     Vector3 vect = center;    //Start point is center point.
         lineRenderer.SetPosition(0, vect);

     for(int x = 0; x < points + 1; x++)
     {
         vect = center;
             vect.x += (float)(Math.Cos(radianFract * x) * Range);
             vect.z += (float)(Math.Sin(radianFract * x) * Range);

         lineRenderer.SetPosition(x + 1, vect);    //Skip first/last points.
     }
     
     vect = center;
     lineRenderer.SetPosition(points + 2, vect);    //Last point is center point.
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Oct 19, 2011 at 08:13 PM

Have the cone pointing away from you on the Z axis. Then use:

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

Comment
Add comment · Show 1 · Share
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
avatar image RogueDeus · Oct 20, 2011 at 04:58 PM 0
Share

Thanks, but I am actually trying to deter$$anonymous$$e how to point the cone to begin with. Once I know, I can find the target angels fine.

I managed to brute force myself a solution, but I would love a more elegant one.

I am currently taking the transform.right of the Camera (as its always locked in rotation) and comparing it to the transform.forward of the attacking unit. Then using that angle as an offset for the cone plot loop.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with casting and drawing multiple raycast along an angle 0 Answers

Adjusting an angle of a collider2D to a linerenderer that is user-created 0 Answers

How to edit Direction Angle using GetAxis 2.5D 8 direction movement 1 Answer

spotAngle won't set Cookie Size on Directional Light? 1 Answer

Colliding Line render or Raycasts 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges