Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
4
Question by Crayz · Jun 23, 2014 at 01:36 AM · raycastvector3quaternionangleeuler

Raycasting at an offset angle?

Hello folks, I'm quite new to Unity3d as well as C# Quaternions, Eulers, and Vector3's. What I'm trying to accomplish is a Raycast at transform.forward with two additional Rays, each angled by a set amount of degrees from the original straight Ray. Here's a photo that should explain what I mean:

alt text

The red ray is the original ray positioned at transform.position, using spawn.forward as direction. Now I'm trying to create an additional ray on each side angled by 15 degrees.

I've done some reading and have a better understanding of what I think needs to be done but just can't figure it out. I have my original ray set up like this:

 Ray ray = new Ray (transform.position, transform.forward);

Thanks for reading!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by Crayz · Jun 23, 2014 at 04:25 AM

Well after a few hours and a little break in between I figured out what I need to do. For anybody else curious simply just multiply the vector by a quaternion along a single axis. There seems to be a few other methods of angling vectors (Euler angle?) though I'm not too sure about it yet. If anybody has suggestions please post

 Vector3 noAngle = spawn.forward;
 Quaternion spreadAngle = Quaternion.AngleAxis(-15, new Vector3(0, 1, 0));
 Vector3 newVector = spreadAngle * noAngle;
 
 Ray ray = new Ray (spawn.position, newVector);
 
 CastProjectiles(shotDistance, ray);
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
2

Answer by cosmin2dor · Aug 23, 2017 at 10:44 AM

While every other answer is valid for this particular question, I want to answer you more general. So, everytime when you need to rotate a vector by a certain amount of degrees you should look into Rotation Matrix. Just google it for details. alt text

Remeber that the angle is in Radians form here. Now in order to compute the rotation, you have to multiply your original vector with this R (R*V not V*R!). After multiplication you should get this alt text

Now the vector [x', y'] is the rotated version of the original [x, y]. All you have to do in code is to right the two equations and transform the angle input into Radians. This exemple applys only in 2D, if you want more axes, the concept is the same, except bigger Matrices. Check it out on Wikipedia: https://en.wikipedia.org/wiki/Rotation_matrix

Also you should notice that this is a really powerful approach and it can be used at any sort of vector rotation and in multiple dimensions.


matrix2.png (31.7 kB)
matrix.png (7.7 kB)
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
1

Answer by legionair4 · Dec 02, 2020 at 02:54 AM

Easier solution here: https://answers.unity.com/questions/146975/how-to-raycast-on-45-degree.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 skinnywhitegamedev · Mar 15, 2016 at 08:51 PM

The simplest way I've found to accomplish this is to declare the offset vector3's by multiplying your quaternion angle axes by your forward axis, offsetting in the process from the up axis:

 //Setting up Vector3's for rays
 Vector3 rayPosition = new Vector3(transform.position.x, headHeight, transform.position.z);
 Vector3 leftRayRotation = Quaternion.AngleAxis(-fovAngle, transform.up) * transform.forward;
 Vector3 rightRayRotation = Quaternion.AngleAxis(fovAngle, transform.up) * transform.forward;
 
 //Constructing rays
 Ray rayCenter = new Ray(rayPosition, transform.forward);
 Ray rayLeft = new Ray(rayPosition, leftRayRotation);
 Ray rayRight = new Ray(rayPosition, rightRayRotation);
 
 //(Doing things with rays...)

I then messed with the exposed "headHeight" and "fovAngle" variables in the editor.

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

How to raycast using a vector3 and a rotation? 1 Answer

Constructing a Vector in 2-space from an angle for camera panning 1 Answer

raycast and trigonometry 2 Answers

Rotate vector around vector? 2 Answers

making a camera that moves depending on mousepos.y 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