I have a custom character and I am setting it up so that W makes it go forward, S backwards, A and D change player yaw and mouse or right stick control player look. They work independently of each other. I achieved that, however it only works while the character is moving (W,A,S or D are pressed down). No idea how I managed to do that… I am a software engineer but new to UE4 programming as well as rusty with C++. Thanks in advance!
// Fill out your copyright notice in the Description page of Project Settings.
#include "Game.h"
#include "MyCharacter.h"
#include <Game/Components/MyCharacterMovementComponent.h>
AMyCharacter::AMyCharacter(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<UMyCharacterMovementComponent>(AMyCharacter::CharacterMovementComponentName))
{
// Set this character to call Tick() every frame.
//PrimaryActorTick.bCanEverTick = true;
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
BaseLookRightRate = 45.f;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
PlayerCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
PlayerCameraComponent->SetRelativeTransform(FTransform(FVector(0.0f, 0.0f, 100.0f)));
PlayerCameraComponent->RelativeLocation = FVector(0, 0, 50.0f + BaseEyeHeight);
PlayerCameraComponent->bUsePawnControlRotation = false;
PlayerCameraComponent->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Hellow World from AMyCharacter!"));
}
}
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// Set up gameplay key bindings
check(InputComponent);
InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAxis("Run", this, &AMyCharacter::Run);
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "turn" handles devices that provide an absolute delta, such as a mouse.
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
InputComponent->BindAxis("TurnRate", this, &AMyCharacter::TurnAtRate);
InputComponent->BindAxis("LookUp", this, &AMyCharacter::LookUp);
InputComponent->BindAxis("LookUpRate", this, &AMyCharacter::LookUpAtRate);
InputComponent->BindAxis("LookRight", this, &AMyCharacter::LookRight);
//InputComponent->BindAxis("LookRightRate", this, &AMyCharacter::LookRightAtRate);
// handle touch devices
InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AMyCharacter::TouchStarted);
}
//Input functions
void AMyCharacter::Run(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
void AMyCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
{
// jump, but only on the first touch
if (FingerIndex == ETouchIndex::Touch1)
{
Jump();
}
}
void AMyCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}
void AMyCharacter::LookUpAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
}
void AMyCharacter::LookUp(float Value)
{
if (Value != 0.f && PlayerCameraComponent)
{
PlayerCameraComponent->RelativeRotation.Pitch = PlayerCameraComponent->RelativeRotation.Pitch + Value;
}
}
void AMyCharacter::LookRight(float Value)
{
if (Value != 0.f && PlayerCameraComponent)
{
PlayerCameraComponent->RelativeRotation.Yaw = PlayerCameraComponent->RelativeRotation.Yaw + Value;
}
}