So I can’t use AddControllerYawInput because I need to rotate to an exact angle. I am using code and a blueprint
the code:
header
#pragma once
#include <math.h>
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Soldier.generated.h"
UCLASS()
class TUNNEL_GAME2_API ASoldier : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ASoldier();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool goingForward;
float forwardMovement;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FRotator charRot;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void goForward();
void rotateLeft();
void rotateRight();
void turnBack();
void backRelease();
private:
float currot;
int nextRot;
float rotSpeed;
bool rotatingLeft;
bool rotatingRight;
bool turnedBack;
};
Source:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Soldier.h"
// Sets default values
ASoldier::ASoldier()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
charRot = FRotator(0, 0, 0);
rotSpeed = 45;
nextRot = 0;
currot = 0;
}
// Called when the game starts or when spawned
void ASoldier::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASoldier::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (rotatingRight) {
currot += DeltaTime * rotSpeed;
currot = remainder(currot, 360);
if (currot >= nextRot) {
currot = nextRot;
rotatingRight = false;
}
charRot = FRotator(0, currot, 0);
}
if (rotatingLeft) {
currot -= DeltaTime * rotSpeed + 360;
currot = remainder(currot, 360);
if (currot <= nextRot) {
currot = nextRot;
rotatingLeft = false;
}
charRot = FRotator(0, currot, 0);
}
}
// Called to bind functionality to input
void ASoldier::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("GoForward", IE_Pressed, this, &ASoldier::goForward);
PlayerInputComponent->BindAction("RotateRight", IE_Pressed, this, &ASoldier::rotateRight);
PlayerInputComponent->BindAction("RotateLeft", IE_Pressed, this, &ASoldier::rotateLeft);
PlayerInputComponent->BindAction("TurnBack", IE_Pressed, this, &ASoldier::turnBack);
PlayerInputComponent->BindAction("TurnBack", IE_Released, this, &ASoldier::backRelease);
}
void ASoldier::goForward() {
if (turnedBack || rotatingRight || rotatingLeft) return;
goingForward = true;
}
void ASoldier::rotateLeft() {
if (turnedBack || rotatingRight || goingForward) return;
rotatingLeft = true;
nextRot = (nextRot + 270) % 300;
}
void ASoldier::rotateRight() {
if (turnedBack || rotatingLeft || goingForward) return;
rotatingRight = true;
nextRot = (nextRot + 90) % 300;
}
void ASoldier::turnBack() {
if (rotatingLeft || rotatingRight) return;
turnedBack = true;
}
void ASoldier::backRelease() {
turnedBack = false;
}
the blueprint: