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 TinSleeves · Apr 11, 2013 at 10:58 PM · rotationraycastrotateraycastingrotating

Raycasting, rotating a ray 360 degrees

I've looked but couldn't find an answer to this question so apologies if its already been answered before and I missed it!

I'm working on a 2D game and basically I want to cast a ray and have that ray rotate 360 to check if it touches the player. Hopefully this image will make it obvious what I'm talking about

alt text

I know how to cast rays and check for collision like this:

 void ShootRay()
     {
         origin1 = transform.position;
         RaycastHit hit;
         
         if (Physics.Raycast(origin1, direction, out hit))
         {
             Debug.DrawLine(transform.position, hit.point, Color.green);
         }
     }

I just can't for the life of me get this thing to rotate, so any help would be great

Thanks

raycast.png (28.5 kB)
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

2 Replies

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

Answer by robertbu · Apr 11, 2013 at 11:33 PM

I can think of three ways off the top of my head. I don't know which is the most efficient way (or if efficiency is a concern). One simple way is to use a polar to rectangular conversion. Assuming the above is on the x,y plane and 0 on the z axis, you can construction your your direction like:

 Vector3 direction = new Vector3(Cos(angle), Sin(angle), 0);

'angle' needs to be in radians. If you are working with degrees in your for loop, you can use Mathf.Deg2Rad to make the conversion.

Another way would be to rotate the object this script is attached to (or an empty game object). Again assuming the x,y plane, you can do:

 transform.Rotate(0.0f, 0.0f, delta_degree).
 direction = transform.up;

Transform.Rotate() does an incremental rotation.

The third way use Quaternion.AngleAxis().

 Quaternion q = Quaternion.AngleAxis(delta_degree, Vector3.forward);
 direction = Vector3.up;

Each time you want to increment the angle, do:

 direction = q * direction;

This code rotates the direction vector by delta_degree degrees each time it is executed.

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 TinSleeves · Apr 12, 2013 at 12:04 AM 0
Share

Wow thanks for the detailed answer, all three suggestions were extremely helpful. I went with the third method in the end because it seemed to suit my situation best.

Thanks again

avatar image
2

Answer by mmk · Nov 12, 2015 at 04:24 PM

For any one else looking i was trying this and to rotate around i did this,

 Quaternion q = Quaternion.AngleAxis(100*Time.time, Vector3.up);
 Vector3  d = transform.forward *20;
 Debug.DrawRay (transform.position,q*d,Color.green);





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

12 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

Related Questions

How can i follow an object by 1-axis-rotation? 0 Answers

Rotation for 180 degrees of character 1 Answer

raycast to determine pivot 1 Answer

Make an object translate/raycast a certain way disregarding it's rotation 1 Answer

How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 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