# Add torque to static mesh component in direction camera is facing. C++

**URL:** <https://forums.unrealengine.com/t/add-torque-to-static-mesh-component-in-direction-camera-is-facing-c/412524>\
**Category:** Asset Creation\
**Tags:** torque, unreal-engine, question, CPP\
**Created:** [December 18, 2017, 4:23am UTC](https://forums.unrealengine.com/t/add-torque-to-static-mesh-component-in-direction-camera-is-facing-c/412524 "2017-12-18T04:23:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piccadi11y](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Piccadi11y](https://forums.unrealengine.com/u/Piccadi11y)\
**Post date:** [December 18, 2017, 4:23am UTC](https://forums.unrealengine.com/t/add-torque-to-static-mesh-component-in-direction-camera-is-facing-c/412524/1 "2017-12-18T04:23:31Z")

</div>

While relatively simple logic I’ve spent the last two days trying to figure it out and can’t for the life of me. I’m working with a custom adaption of the rolling template basing some of my code on that provided. One of the differences in control that I’m trying to implement is having torque added to the mesh in the direction that the camera is pointing, rather than simply along the X, Y axes. The pawn I’ve created has the same hierarchy as the one in the template (StaticMeshComponent → SpringArmComponent → CameraComponent) and I have all controls handled through a PlayerController as opposed to the the Pawn itself as is done in the template. Everything but my MoveForward() and MoveRight() functions work as expected. The two where torque should be applied to the StaticMeshComponent. What I can’t figure is how to use the Camera’s forward vector to apply torque to the StaticMeshComponent in the corresponding directions: MoveForward() - forwards and backwards relative to the Camera’s forward vector, MoveRight() left and right perpendicular to the Camera’s forward vector.

Here are the [.h](https://drive.google.com/open?id=1wKP-8rlj7SpIeqNajYp5tCWhvikcjd0Y) and [.cpp](https://drive.google.com/open?id=1ZsBkFAKXdEGnfzixGmKrpcnImM7mq_mJ) file for this pawn. The code in MoveForward() (other than the if-statements) are my lackluster attempts at getting it to work, the block of commented code is split into different iterations with the use of multiple comment blocks/lines.

Any help figuring this out would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_49a519741](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_49a519741](https://forums.unrealengine.com/u/anonymous_user_49a519741)\
**Post date:** [December 18, 2017, 5:18am UTC](https://forums.unrealengine.com/t/add-torque-to-static-mesh-component-in-direction-camera-is-facing-c/412524/2 "2017-12-18T05:18:12Z")

</div>

If you want the torque to move you forward then you should use the “right vector” as the input axis – like a wheel axle. Forward/backward will be positive/negative values or vice versa – not sure which.

So the torque vector should just be GetRightVector() from the camera (tho if you’re doing the camera right then I’d use GetControlRotation() and get a forward vector from that). Then multiply that vector by the axis value and apply the torque.

And for right movement, use the forward vector (again, left/right will depend on the sign, not sure which but i bet right is negative).

I think that’s what you’re asking and what the docs say.

---

<div class="post-metadata">

**Author:** ![Piccadi11y](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Piccadi11y](https://forums.unrealengine.com/u/Piccadi11y)\
**Post date:** [December 18, 2017, 5:47am UTC](https://forums.unrealengine.com/t/add-torque-to-static-mesh-component-in-direction-camera-is-facing-c/412524/3 "2017-12-18T05:47:04Z")

</div>

“Like a wheel axle.” Exactly what I was looking for, thank you. As you can probably see in the files I had the GetRightVector() and GetForwardVector() in the opposite functions to where they should have been. Works perfectly now.
