# Rotate component in it's local space toward a point in world

**URL:** <https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388>\
**Category:** Programming & Scripting\
**Tags:** rotation, rotation-direction, unreal-engine, rotate, question, CPP, vector-math\
**Created:** [June 18, 2024, 7:12am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388 "2024-06-18T07:12:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 7:12am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/1 "2024-06-18T07:12:29Z")

</div>

Hi.  
I apologize for the hackneyed question, but I couldn’t find the answer on the forum.  
I’m making a tank.  
Simple hierarchy: Body → tower → cannon.  
A little stuck on the aiming system.

I’m doing a linetrace from the screen to the world. A hit position appears in the world. I need the tower to rotate to this point around its local Z axis, and the gun along Y;

A static turret is very easy to implement, since I can use world coordinates, but here I need to rotate this components relative to the body of the tank, since when moving on inclined surfaces, the rotation is incorrect if I use world coordinates.

If possible, an example in C++ would be very helpful, since I’m not very good at blueprints…

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 7:58am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/2 "2024-06-18T07:58:33Z")

</div>

I’m assuming you would use the same setup in CPP as BP, which would be ‘look at rotation’. But for this to work, your gun needs to point along the local X axis.

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 8:06am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/3 "2024-06-18T08:06:31Z")

</div>

Yes, I see that “FindLookAtRotation” simply subtracts the “start” and “target” vectors to find the direction.  
What should I do with this direction then?)

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 8:10am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/4 "2024-06-18T08:10:09Z")

</div>

That’s the direction you point the turret, but only Z

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/5/3e5c158daf333d3724377fb38997fb4cf86ea47c.png)

( sorry for the BP, I don’t ‘do’ CPP 🙂 )

But like I say, this node will point the forward vector of the component at the location, so you need the pivot to be setup correctly.

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 8:28am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/5 "2024-06-18T08:28:34Z")

</div>

![изображение](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/d/2/6d226ddafa3c868f9d0fed561508b461173ef880.png)

In C++ it looks something like this, but unfortunately it doesn’t work. The turret rotates to the desired point only if the tank itself is pointed towards the world X axis. When the tank rotates 90 degrees, the turret also looks to the target direction in the direction, but with + or - 90 degrees and so on…(

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 8:41am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/6 "2024-06-18T08:41:12Z")

</div>

Notice a rotator is returned from the node. Does it really just subtract one vector from the other?

( I don’t have the source )

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 8:47am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/7 "2024-06-18T08:47:54Z")

</div>

![изображение](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/a/51a04aa639a995ff81141677eafdfef8147055b0.png)

Yes, indeed… It returns “FRotationMatrix”) I’ll see what i can do with this matrix)

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 8:53am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/8 "2024-06-18T08:53:38Z")

</div>

Gimme a minute… 🙂

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 8:58am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/9 "2024-06-18T08:58:44Z")

</div>

So I have a target actor in the world, and this code

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/3/b/73bcee78ed41c271c1aff06647a9206f0e6be77c.png)

![lookat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/a/8/9a8ab72e3651f328fa60c173dab9850c12a03a1a.gif)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/2/f/82f780bfc52274d7b8eea405231f4b8bf8112ecf.png)

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 10:09am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/10 "2024-06-18T10:09:10Z")

</div>

Yes, as I already said, what you show is very easy to do, since these are two actors, and they are both located in world coordinates. The only thing is that I need to rotate not a separate actor, but a component that is lower in the hierarchy of the main actor itself… The hierarchy is like this:

Tank  
|  
Tower - Rotate along local Z towards the target  
|  
Cannon - Rotate along local Y towards the target

The tank also rotates when it moves…)  
This method does not work in the local coordinates of the component…(

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 10:10am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/11 "2024-06-18T10:10:03Z")

</div>

I am rotating a component… I am pointing the component at a world location.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/6/f/86fd84c75643adabb3de4383b572069c9f134cc5.jpeg)

![tank](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/9/5/2959431dae68f8903750d120295701f0fe26527c.gif)

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 10:19am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/12 "2024-06-18T10:19:41Z")

</div>

But an actor is not a component…  
You can clearly see SetActorRotation() in your screenshot)  
Or am I missing something?)

Will the result be the same even if you rotate around various axes the tank itself?)

By the way. Please tell me how you make such small videos? What soft you are using for this?) It would probably be very cool and simple if I could do the same to show what exactly I’m doing)

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [June 18, 2024, 10:27am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/13 "2024-06-18T10:27:21Z")

</div>

You’re right, my CPP example only contains actors, sorry. I’ll see if I can find something more relevant… But it will be ‘set relative rotation’ on the component.

I use

> **[ScreenToGif - Record your screen, edit and save as a gif or video](https://www.screentogif.com/)**
>
> Free screen recorder tool. Record, edit and save as a gif or video.

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 10:31am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/14 "2024-06-18T10:31:47Z")

</div>

Now I’ll try to record a video and show what exactly I mean 🙂

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 11:28am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/15 "2024-06-18T11:28:11Z")

</div>

![изображение](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/1/f/11f3cf25b4f9b50c7354aebc7b524b409a529115.png)

Sorry, I can’t load the video… The Internet connection is interrupted all the time.

Alas, “SetRelativeRotation” preserves rotation in other axes but sets the Z rotation in the target direction incorrectly, and “SetWorldRotation” rotates in the target direction correctly, but does not prevent rotation in other axes…(

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 18, 2024, 11:33am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/16 "2024-06-18T11:33:09Z")

</div>

> Rotate component in it’s local space toward a point in world

See if this is what you’re looking for:

> [@Restrict Pawn's Rotation](https://forums.unrealengine.com/t/restrict-pawns-rotation/487856/3):
>
> Have a look at this one: Script to copy: [[Image from Gyazo]](https://gyazo.com/07facc8d174c94dd6103a641878c6929) Essentially, Dot Product is an answer to most angular issues. The yellow arrow indicates the turret’s front. Oh, and a TL is probably not the way to go about it unless you’re after something truly specific / unique / inflexible. Does not mesh well with target tracking in my head.

You must transform direction to / from local space. Ignore the interps and clamps, ofc. It’s a bit dated, today I’d at least `slerp` directions.

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 11:44am UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/17 "2024-06-18T11:44:24Z")

</div>

Well, this will take some time)  
I’ll be sure to answer as soon as I get something done)  
Thank you in advance. From local to world and back looks very logical in this situation. I hope it will work 🙂

---

<div class="post-metadata">

**Author:** ![Chaotic\_Ray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chaotic_ray/32/571779_2.png) [@Chaotic\_Ray](https://forums.unrealengine.com/u/Chaotic_Ray)\
**Post date:** [June 18, 2024, 12:22pm UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/18 "2024-06-18T12:22:18Z")

</div>

Perfectly works)  
Just awesome! This is the very first time that someone was really able to help me)  
Eternally grateful to you)  
The code looks like this) I hope this will help someone else too)

 ![изображение](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/4/c/64c56d6b5b9bfe7b2106d93af1e68de597c1a5f0.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 18, 2024, 12:25pm UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/19 "2024-06-18T12:25:33Z")

</div>

Neater than BP spaghetti, for sure!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 18, 2024, 12:26pm UTC](https://forums.unrealengine.com/t/rotate-component-in-its-local-space-toward-a-point-in-world/1907388/20 "2024-07-18T12:26:25Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
