- Home /

# 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:

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!

**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);
```

**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.

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

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.

**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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

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

how to convert the angle to roatation? 1 Answer

Angles from quaternion/vector Problem. 2 Answers

Angle between Ray and Normal 1 Answer

Problem With Raycasthit Angle 0 Answers