- Home /
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.
}
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
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

Follow this Question
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