Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Dragonfly3r · Jan 05, 2017 at 06:54 AM · c#rotationraycastraycasthitkeyboard input

How can you rotate a raycast on input?

So I'm trying to make raycast script sort of like a sonar whereby if a enemy comes into range the raycast will do a sweep across a number of raycast lines at different angles and back again at around 60fps

For the time being until I can figure out how to do this automatically. I want to find out how it is possible to make a raycast when you put input it will rotate.

An example of this would be the second hand on a clock face. for every time a gear in the clock shifts it moves to the next position.

What I want to do is the same but replace the gear with keyboard input.

the Angle of the raycast would for example start as <----

I then press keyboard key once and the raycast would change rotation to \ I press the keyboard key once again and the raycast would change rotation to | Press it again and it would change rotation to / Finally press is once again and it would change rotate to -->

Then it would go back in the reverse direction on next key input

At the later point in time I want to be able to automate this process and also want to be able to set it so you can create multiple rotation positions for that single raycast.

How would I go about create something like this?

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 romatallinn · Jan 05, 2017 at 08:06 AM

Well... A bit confusing explanation of the task. Hope I didn't misunderstand anything.

For raycatsing. Example is given for 3D. If you make a 2D game, then a bit different axis must be used -

 public float angle;
 
 //---------

 // In this case, final dir is related to player's rotation. 
 // If you want to relate it to the global axis - use just Vector3.forward instead
 Vector3 initDir = transform.forward;


 Quaternion angleQ = Quaternion.AngleAxis(angle, Vector3.up); // or use instead of up, the angle's axis you use
 Vector3 newVector = angleQ * initDir;
  
 Ray ray = new Ray (transform.position, newVector);

So, if you want to change angle with each input, then just add/decrease some value from the var "angle".

For automating the process, I would use coroutines. Inside a coroutine a loop. You add/decrease some value from the angle and WaitForSeconds if you want to make it change gradually.

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

8 People are following this question.

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

Related Questions

Help finding out which side of a cube a raycast hits 1 Answer

Flip over an object (smooth transition) 3 Answers

C# raycast shooting script broken 1 Answer

Raycast problem 1 Answer

Having a RaycastHit event create a component? 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