# Rotating a vector

**URL:** <https://forums.unrealengine.com/t/rotating-a-vector/25798>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 29, 2015, 2:37pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798 "2015-03-29T14:37:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scienziatogm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scienziatogm/32/906600_2.png) [@Scienziatogm](https://forums.unrealengine.com/u/Scienziatogm)\
**Post date:** [March 29, 2015, 2:37pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/1 "2015-03-29T14:37:46Z")

</div>

Hi, I need to rotate a vector to do a shotgun instant hits system. I did this to rotate each hit horizontally:

```auto

GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, ((CameraForwardVector * 50000) + CameraWorldPosition).RotateAngleAxis(5.f, FVector(0.f, 0.f, 1.f)), ECC_Visibility, TraceParams);

```

but the problem is that if I change the X or Y axis instead of Z, to rotate a vector of a hit vertically, it goes up if I’m watching a side of the map, and it goes down if I’m watching the other side.

What can I do to rotate the vector of a hit vertically?

---

<div class="post-metadata">

**Author:** ![Zeb89](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeb89](https://forums.unrealengine.com/u/Zeb89)\
**Post date:** [March 29, 2015, 4:53pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/2 "2015-03-29T16:53:00Z")

</div>

![bullets1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/2/b/2bff830d913d3ae08d966b0834a8d529bfa7f1c2.jpeg)  
So basically we attempted to set up our shotgun bullet spread using vectors with coordinates this way (see attached image).  
Our issue is that we can’t seem to get our vectors to rotate along the x axis. Only the Z axis seems to work, therefore our bullet spread system is only half done.

Does someone know why this is happening and/or what could possibly be causing this?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4fe9f58b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4fe9f58b1](https://forums.unrealengine.com/u/anonymous_user_4fe9f58b1)\
**Post date:** [March 29, 2015, 5:20pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/3 "2015-03-29T17:20:55Z")

</div>

Better start working with angles and rotators. Shotgun spread is more easily represented in terms of angles, so you just create a rotator with the yaw and pitch of whatever spread you want. FRotator::RotateVector will transform a vector, so you can just take your camera forward vector and transform it using each shotgun spread rotator.

Also, you’d probably want to randomize shotgun spread, rather than use predefined coordinates. You can use FMath::VRandCone for that. Let’s say you want your maximum shotgun spread to be 2 degrees:

```auto

FVector PelletDir = FMath::VRandCone( CameraForwardVector, FMath::DegreesToRadians( 2.0f ) );
GetWorld()->LineTraceSingle( GunHitResult, CameraWorldPosition, CameraWorldPosition + PelletDir * MaxPelletDistance, ECC_Visibility, TraceParams);

```

-

---

<div class="post-metadata">

**Author:** ![Zeb89](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeb89](https://forums.unrealengine.com/u/Zeb89)\
**Post date:** [March 29, 2015, 5:46pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/4 "2015-03-29T17:46:45Z")

</div>

Thank you very much for the heads up.🙂 Since our game is an FPS arena we’re going for the fixed spread method, Quake style.

We’ll try out your solution and report back.

---

<div class="post-metadata">

**Author:** ![Scienziatogm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scienziatogm/32/906600_2.png) [@Scienziatogm](https://forums.unrealengine.com/u/Scienziatogm)\
**Post date:** [March 29, 2015, 5:58pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/5 "2015-03-29T17:58:57Z")

</div>

> [@cmartel;259646](#):
>
> Better start working with angles and rotators. Shotgun spread is more easily represented in terms of angles, so you just create a rotator with the yaw and pitch of whatever spread you want. FRotator::RotateVector will transform a vector, so you can just take your camera forward vector and transform it using each shotgun spread rotator.
> 
> Also, you’d probably want to randomize shotgun spread, rather than use predefined coordinates. You can use FMath::VRandCone for that. Let’s say you want your maximum shotgun spread to be 2 degrees:
> 
> ```auto
> 
> FVector PelletDir = FMath::VRandCone( CameraForwardVector, FMath::DegreesToRadians( 2.0f ) );
> GetWorld()->LineTraceSingle( GunHitResult, CameraWorldPosition, CameraWorldPosition + PelletDir * MaxPelletDistance, ECC_Visibility, TraceParams);
> 
> ```
> 
> -

Same problem. If I watch this side of the map, I get this:

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/d/2/9d246d3da03386e4d7da42cc554d598d15cf11fe.jpeg)  
else if I watch the other side of the map, I get this:

[http://puu.sh/gUiGm/f8ba26c559.jpg](http://puu.sh/gUiGm/f8ba26c559.jpg)

If I turn 90 degrees left or right, the point is in the center of the camera.

I did this:

```auto

GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, FRotator(5.f, 0.f, 0.f).RotateVector(((CameraForwardVector * 50000) + CameraWorldPosition)), ECC_Visibility, TraceParams);

```

Why it isn’t working?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4fe9f58b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4fe9f58b1](https://forums.unrealengine.com/u/anonymous_user_4fe9f58b1)\
**Post date:** [March 29, 2015, 6:22pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/6 "2015-03-29T18:22:59Z")

</div>

```auto

FRotator(5.f, 0.f, 0.f).RotateVector( **((CameraForwardVector * 50000) + CameraWorldPosition)** )

```

You’re rotating both your origin and your direction, I doubt that helps.

```auto

CameraWorldPosition + FRotator(5.f, 0.f, 0.f).RotateVector( **CameraForwardVector** ) * 50000

```

See if that helps. By the way, this is just stylistic advice but you have much to gain and nothing to lose by separating this kind of stuff over multiple lines of code. Rather than cram everything on a single line, it’s a lot more readable to calculate each vector separately before passing it to your trace. Readability helps avoiding precisely this sort of issue.

And you mention Quake as a reference, I’m pretty certain Quake 3 randomizes its pellet spread. 🙂

-

---

<div class="post-metadata">

**Author:** ![Scienziatogm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scienziatogm/32/906600_2.png) [@Scienziatogm](https://forums.unrealengine.com/u/Scienziatogm)\
**Post date:** [March 29, 2015, 6:29pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/7 "2015-03-29T18:29:47Z")

</div>

> [@cmartel;259681](#):
>
> ```auto
> 
> FRotator(5.f, 0.f, 0.f).RotateVector( **((CameraForwardVector * 50000) + CameraWorldPosition)** )
> 
> ```
> 
> You’re rotating both your origin and your direction, I doubt that helps.
> 
> ```auto
> 
> CameraWorldPosition + FRotator(5.f, 0.f, 0.f).RotateVector( **CameraForwardVector** ) * 50000
> 
> ```
> 
> See if that helps. By the way, this is just stylistic advice but you have much to gain and nothing to lose by separating this kind of stuff over multiple lines of code. Rather than cram everything on a single line, it’s a lot more readable to calculate each vector separately before passing it to your trace. Readability helps avoiding precisely this sort of issue.
> 
> And you mention Quake as a reference, I’m pretty certain Quake 3 randomizes its pellet spread. 🙂
> 
> -

I did this:

```auto

GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, CameraWorldPosition + FRotator(5.f, 0.f, 0.f).RotateVector(CameraForwardVector * 50000), ECC_Visibility, TraceParams);

```

but the problem is still there.

---

<div class="post-metadata">

**Author:** ![Zeb89](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeb89](https://forums.unrealengine.com/u/Zeb89)\
**Post date:** [March 29, 2015, 6:34pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/8 "2015-03-29T18:34:41Z")

</div>

The issue seems to be that the vectors are erroneously influenced by the direction you are aiming at. We need to find a way to fix this issue which could be ovverriding the rotation set for the vectors.

---

<div class="post-metadata">

**Author:** ![Zeb89](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeb89](https://forums.unrealengine.com/u/Zeb89)\
**Post date:** [March 30, 2015, 4:22pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/9 "2015-03-30T16:22:44Z")

</div>

Can someone please help us out? This issue is killing our workflow. ☹

---

<div class="post-metadata">

**Author:** ![Scienziatogm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scienziatogm/32/906600_2.png) [@Scienziatogm](https://forums.unrealengine.com/u/Scienziatogm)\
**Post date:** [March 30, 2015, 11:45pm UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/10 "2015-03-30T23:45:19Z")

</div>

If I use the local position, it works. Thanks anyway.

---

<div class="post-metadata">

**Author:** ![asperatology](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@asperatology](https://forums.unrealengine.com/u/asperatology)\
**Post date:** [March 31, 2015, 12:35am UTC](https://forums.unrealengine.com/t/rotating-a-vector/25798/11 "2015-03-31T00:35:30Z")

</div>

This is actually unrelated to your code and your problem, which I can’t help to fix, but this is relevant to the topic, which is to rotate a vector. Future readers who are looking to rotate a vector, this is how to do it:

```auto

//Try to get a rotator from anywhere that you want your vector to face towards.
//In this case, I used my UStaticMeshComponent rotation of my AActor.
FRotator Rotation = this->MyMesh->GetComponentRotation();

//This is an arbitrary target vector you want to rotate. It can be any vector you designate.
FVector Vector = FVector(1.0f, 0.0f, 0.0f);

//To rotate, I call on FRotationMatrix class, pass in the rotator, and call on RotateVector()
//to rotate a vector for me. Pass it in like this:
Vector = Rotation.RotateVector(Vector);

```
