- Home /
Firing rays in a cone shape from the player
Although some of my code seems to work, I am getting some unexpected results.
I have a for loop in which I create a new ray each time in a specific direction - which is where the problem lies. I want to trace a cone shape from the player in its forward direction. However, while the rays are cast in a circle or cone shape, the width or angle of this cone changes as the object moves rather than being set to a specific angle.
If I want to trace a cone shape with an angle of 30d from the center, it traces a cone shape centered around 30d, on the x axis in my case, instead. The angle changes as it moves.
How can I make it trace the cone shape specified by my angle around the forward direction?
//search in circle around center
float stepAngleDeg = 360 / subdivisions; //angle between two sampled rays
for (int i = 0; i < subdivisions; i++)
{
Vector3 localRotation = new Vector3(angleFromCenter, 0, i * stepAngleDeg); //direction of Ray to cast in Euler XYZ form
Vector3 direction = (Quaternion.Euler(localRotation) * transform.up).normalized; //turn rotation into direction
Ray ray = new Ray (bulletSpawnPoint.position, direction);
Debug.DrawLine(ray.origin, ray.origin + (direction * asteroidAvoidDistance), Color.red);
}
What if I want the opposite thing? I mean I have the apex point and the adiacent, how can I trace the circle?
Answer by Hellium · Jun 13, 2015 at 07:34 PM
If you want to trace a cone, I would suggest to "construct" your cone as follow :
You know the main axis of your cone, thanks to the transform.forward of your spawn point.
Then, you also have two vectors belonging to the base of your cone : transform.up and transform.right.
You have the radius of your cone (I guess) and your step angles (in radians). Thus, you can draw a circle like the Unit Circle Trigonometry : transform.right cos(angle) r + transform.up sin(angle) r. You now have a set of points, you have the apex of your cone, you can have a vector ! This vector is the direction + length of your ray.
EDIT : If you want your radius, you will have to use the following formula :
You know that tan α = opposite / adjacent where α is you angle. You want opposite (you already know adjacent, which is the height of your cone).
Thus, opposite = tan α x adjacent
Though, I would recommend to use a solid mesh as a trigger to detect a collision from your bullet. (Just make it on a 3D software like blender, it takes 2 minutes, export it in fbx, remove mesh renderer, add mesh collider)
Sorry, I use transform.up because I am not importing my objects manually, but have rather just saved the blend files into the unity folder. Due to the Z and Y axis being swaped I use the up direction as forward.
I have rewritten the code as you said and that almost works, but again I get some weird results when trying to convert to the transform direction. I think the problem lies in the models being used from blender without being exported properly. I guess exporting them in fbx format should solve the problem.
In blender, don't forget when exporting :
]
Give a try with an fbx file and let me know if it has worked ! ;)
Actually, I realised I was using InverseTransformDirection rather simply TransformDirection. So I was changing the direction to global rather than local.
Everything is working now like you said.
Thanks!
What If I want the inverse thing? I mean, I have the apex point and the adiacent. How can I trace the circle?
Answer by Tolufoyeh · Aug 05, 2016 at 10:53 AM
Here's Unity's own tutor's code https://unity3d.com/learn/tutorials/projects/stealth-tutorial-4x-only/enemy-sight
Answer by crare · Jan 24, 2017 at 07:39 AM
You could have cone shaped collider and check if it's not behind something with raycast towards target while onCollision with the collider. https://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html
Answer by okletshavesomefun · Sep 15, 2018 at 10:37 AM
I saw several people looking for a ConeCast, so I made one. It's an extension method to use as Physics.ConeCastAll: https://twitter.com/WalterEllisFun/status/1040202706982502401
Your answer
Follow this Question
Related Questions
What's wrong with my RaycastHit2D? 1 Answer
Raycast returns null for no apparent reason 0 Answers
Ray direction, projection matrix 0 Answers
project a ray in a straight line 1 Answer
Raycast - Making ray shoot down vertically -Vector.up 1 Answer