# How to Set the Actor rotation to Yaw in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, CPP, question\
**Created:** [May 2, 2022, 9:46pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082 "2022-05-02T21:46:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 9:46pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/1 "2022-05-02T21:46:55Z")

</div>

I want to set the rot\_Y to actor rotation,

```auto
float rot_Y = GetControlRotation().Yaw;
SetActorRotation(rot_Y);

```

 ![image_2022-05-03_031508902](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/3/b/33bbf6cd5bde63f5e7c91833b0421e8083a760a6.png)

any help appreciated, Thank You.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 2, 2022, 10:19pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/2 "2022-05-02T22:19:24Z")

</div>

Hi Alexa.kl,

There is a good document here that describes the things you are trying to do.  
It looks like you’ve already got a lot covered, so maybe skip down to section 2.3 - Implementing Character Movement Functions

> **[2 - Implementing your Character](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/CPPTutorials/FirstPersonShooter/2/)**
>
> How to implement your First Person Shooter character.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 10:30pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/3 "2022-05-02T22:30:05Z")

</div>

I have these all setup done, now I am trying to add the free camera look, the player hold the Alt key and move the mouse , he can look around without effecting the character.

When the Alt key is on Hold, the mouse only effect the Yaw .

```auto
void AMyCharacter::FreeLook(float Axis)
{
    _bAltPressed = true;
	AltPressedRotation = SpringArm->GetTargetRotation();
	GetCharacterMovement()->bUseControllerDesiredRotation = false;
	GetCharacterMovement()->bOrientRotationToMovement = true;
}

```

Thank You very much for helping me in my Studies

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 10:32pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/4 "2022-05-02T22:32:15Z")

</div>

![image_2022-05-03_033134797](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/d/8/6d8dac60a6b68ec97a10da6dfa4b771b3adf96b7.png)

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 10:33pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/5 "2022-05-02T22:33:54Z")

</div>

![image_2022-05-03_031508902](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/3/b/33bbf6cd5bde63f5e7c91833b0421e8083a760a6.png)

this I want to check in tick every frame to see if the Alt is pressed.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 2, 2022, 10:58pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/6 "2022-05-02T22:58:35Z")

</div>

I think what you’re after is:

```auto
bAltPressed=MyPlayerController->IsInputKeyDown(EKeys::LeftAlt);

```

Where MyPlayerController is your reference to the PlayerController.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 11:39pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/7 "2022-05-02T23:39:24Z")

</div>

.cpp

```auto
void AMyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (_bAltPressed) //Tick to call sync the character in real time
	{
		SynchCharacterFreeLook();
	}
}

```

```auto
void AMyCharacter::SynchCharacterFreeLook()
{
		print("Synch free look"); //called print on screen macro
		FRotator rotYaw_GC = GetControlRotation();
		FRotator rotYaw_GA = GetActorRotation();
		rotYaw_GA.Yaw = rotYaw_GC.Yaw;
		SetActorRotation(rotYaw_GA);
}

```

```auto
PlayerInputComponent->BindAction(FreeLook, IE_Pressed, this, &AMyCharacter::FreeLookPressed);
PlayerInputComponent->BindAction(FreeLook, IE_Released, this, &AMyCharacter::FreeLookReleased);

```

```auto
void AMyCharacter::FreeLookPressed() //Alt Pressed
{
	_bAltPressed = true;
	FRotator AltPressedRotation = SpringArm->GetTargetRotation();
	GetCharacterMovement()->bUseControllerDesiredRotation = false;
	GetCharacterMovement()->bOrientRotationToMovement = true;
}

void AMyCharacter::FreeLookReleased() //Alt Released
{
	_bAltPressed = false;
	FRotator AltPressedRotation = SpringArm->GetTargetRotation();
	GetCharacterMovement()->bUseControllerDesiredRotation = true;
	GetCharacterMovement()->bOrientRotationToMovement = false;
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 2, 2022, 11:45pm UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/8 "2022-05-02T23:45:51Z")

</div>

This is whole structure , but not working correctly, I have doubt on `SynchCharacterFreeLook()` , something I have done not correctly I think in this function.

---

<div class="post-metadata">

**Author:** ![IBDray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ibdray/32/474116_2.png) [@IBDray](https://forums.unrealengine.com/u/IBDray)\
**Post date:** [May 3, 2022, 12:02am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/9 "2022-05-03T00:02:58Z")

</div>

I guess u need to save current direction of movement when u press Alt key (i.e. in `FreeLookPressed()`) to a variable and use it in movement code instead of calculating new movement direction relative to camera rotation.  
Later (in `FreeLookRelease()`) just return to using calculated direction.

Tick isnt nessesary at all

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 12:12am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/10 "2022-05-03T00:12:50Z")

</div>

Very Good Idea, but for this I will have to modify the code on many places and the main thing how to save the current location in a variable and process it when the mouse rotates?

I am doing this in tick to make it in real time sync.

---

<div class="post-metadata">

**Author:** ![IBDray](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ibdray/32/474116_2.png) [@IBDray](https://forums.unrealengine.com/u/IBDray)\
**Post date:** [May 3, 2022, 12:23am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/11 "2022-05-03T00:23:00Z")

</div>

Hmm… how about create `FVector FreeLookMovementDirection` in .h and save there ur direction in `FreeLookPressed()` method (dont know how u calculate direction, maybe try `Camera->GetForvardVector` for forvard/backward dir vector and `Camera->GetRightMovement` for left/right dir vector) then in move method via `if (bAltPressed)` change between default direction and saved direction

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 3, 2022, 12:28am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/12 "2022-05-03T00:28:09Z")

</div>

What happens if you just have “rotYaw\_GA.Yaw=0;”? One of my favorite ways to test things…

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 12:32am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/13 "2022-05-03T00:32:07Z")

</div>

this way it will work , I already tried but its very heavy on performance because this function is called on minimum 40 places, even called when on air dive or in car drive, it will have a time line too to smooth back to the original position when the alt key released.  
so these 40+ functions executes when the alt key is pressed and tick will progress them.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 12:36am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/14 "2022-05-03T00:36:09Z")

</div>

when I put it to zero, nothing happened, on pressing alt and I have no free look functionality.

I approached something different: Now character is rolling faster fast when the alt is pressed and the mouse moved.

```auto
void AMyCharacter::SynchCharacterFreeLook()
{
		print("Synch free look");
		float rotYaw = GetControlRotation().Yaw;
		FRotator rot = GetActorRotation().Add(0.0f, rotYaw, 0.0f);
		SetActorRotation(rot);

		//FRotator rotYaw_GA = GetActorRotation();
		//rotYaw_GA.Yaw = rotYaw_GC.Yaw;
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 1:17am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/15 "2022-05-03T01:17:42Z")

</div>

> [@Alexa.Ki](#):
>
> `FRotator rot = GetActorRotation().Add(0.0f, rotYaw, 0.0f);`

continuous rotation was caused by the `rotYaw` in `FRotator rot = GetActorRotation().Add(0.0f, rotYaw, 0.0f);`  
Just changed it to `FRotator rot = GetActorRotation().Add(0.0f, 0.0f, 0.0f);` temoporary fixed the bad rotation and now when I hold the alt key, spring arm is rotating as expected when the mouse is moved. So now I can look around and it not effecting the character location/rotation. Only the spring arm rotates under the boundaries

but still I want to set the `SetActorRotation(rot);` as shown in the image

 ![image_2022-05-03_031508902](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/3/b/33bbf6cd5bde63f5e7c91833b0421e8083a760a6.png)

---

<div class="post-metadata">

**Author:** ![Gankable](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gankable](https://forums.unrealengine.com/u/Gankable)\
**Post date:** [May 3, 2022, 1:22am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/16 "2022-05-03T01:22:52Z")

</div>

I think this is what you are trying to achieve. I apologize if I am incorrect.  
Here is how I currently have mine working. I hope it helps!

```auto
InputComponent->BindAction("CameraHold", IE_Pressed, this, &ATopDownPlayerController::HandleCameraHoldStart);

InputComponent->BindAction("CameraHold", IE_Released, this, &ATopDownPlayerController::HandleCameraHoldStop);

```

```auto
void ATopDownPlayerController::PlayerTick(const float DeltaTime)
{
	Super::PlayerTick(DeltaTime);	
	HandleCameraRotation(DeltaTime);
}

```

```auto
void ATopDownPlayerController::HandleCameraRotation(const float DeltaTime)
{
	if (bIsCameraHeld)
	{
		if (const ATopDownCharacter* MyPawn = GetTopDownCharacter())
		{
			if (USpringArmComponent* CameraBoom = MyPawn->GetCameraBoom())
			{
				GetInputMouseDelta(MouseDeltaX, MouseDeltaY);
				CameraBoom->AddRelativeRotation(FRotator(0, MouseDeltaX * PlayerInput->GetMouseSensitivityX() * ( 100.0f + DeltaTime ), 0));
				SetMouseLocation(PreviousMouseX, PreviousMouseY);
			}
		}
	}
}

```

```auto

 void ATopDownPlayerController::HandleCameraHoldStart()
 {
 	bIsCameraHeld = true;
 	bShowMouseCursor = false;
 	GetMousePosition(PreviousMouseX, PreviousMouseY);
 }

```

```auto

void ATopDownPlayerController::HandleCameraHoldStop()
{
	bShowMouseCursor = true;
	bIsCameraHeld = false;
	MouseDeltaX = 0;
	MouseDeltaY = 0;
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 1:33am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/17 "2022-05-03T01:33:06Z")

</div>

This is also a good set of logics, but mine is different and I have it working, the problem is the pitch value stops updating at 90 degrees due to Gimbal Lock.  
I can only pass this if I will manage to set the actor.Yaw from controller.Yaw as shown in the image above.  
In blueprint I can set it and in C++ I have no Idea about it.

---

<div class="post-metadata">

**Author:** ![Gankable](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gankable](https://forums.unrealengine.com/u/Gankable)\
**Post date:** [May 3, 2022, 1:41am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/18 "2022-05-03T01:41:37Z")

</div>

Are you trying to have your character rotate to match the forward vector of the controller? Sorry I’m having a difficult time trying to understand…

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 3, 2022, 1:45am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/19 "2022-05-03T01:45:53Z")

</div>

No, that is already matched, now I am trying to set the controller.Yaw to actor.Yaw while the alt key is released. so all the rotations and other values comes to its original state + character comes back to its state smoothly, for this I will add a float curve with a timeline animation

---

<div class="post-metadata">

**Author:** ![Gankable](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gankable](https://forums.unrealengine.com/u/Gankable)\
**Post date:** [May 3, 2022, 1:46am UTC](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082/20 "2022-05-03T01:46:11Z")

</div>

SetActorRotation(FRotator(0, GetControlRotation().Yaw, 0));

[Next page](https://forums.unrealengine.com/t/how-to-set-the-actor-rotation-to-yaw-in-c/547082.md?page=2)
