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
1
Question by ananion · Jun 13, 2015 at 07:17 PM · raycastingdirectionrayshapecone

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);
         }
Comment
Add comment · Show 1
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 alex920a · Sep 17, 2016 at 04:19 PM 0
Share

What if I want the opposite thing? I mean I have the apex point and the adiacent, how can I trace the circle?

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

alt text

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

alt text

alt text

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)


triangle.png (3.9 kB)
cone.png (6.0 kB)
Comment
Add comment · Show 4 · 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 ananion · Jun 14, 2015 at 02:02 PM 0
Share

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.

avatar image Hellium · Jun 14, 2015 at 02:27 PM 0
Share

In blender, don't forget when exporting :

![alt text]

Give a try with an fbx file and let me know if it has worked ! ;)

avatar image ananion · Jun 14, 2015 at 02:36 PM 1
Share

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!

avatar image alex920a · Sep 16, 2016 at 01:43 PM 0
Share

What If I want the inverse thing? I mean, I have the apex point and the adiacent. How can I trace the circle?

avatar image
0

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

Comment
Add comment · 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
0

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

Comment
Add comment · 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
0

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

Comment
Add comment · 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

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

6 People are following this question.

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

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


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