I have created my custom character in C++ and have it spawning in the world as the default thing that your character is possessing. It has my custom components attached, as in the code you read below you will see the debug lines show up so I do know they are there. When I hit ‘W’ ‘A’ ‘S’ or ‘D’ (I have them set as the axis input in the project settings) it will fire off my custom events shown in the code below. However, no movement is happening. I can’t make it move no matter how hard I’ve tried for the past 7 hours, and scouring the net (long work day, maybe my brain is fried.) I’ve tried SetPhysicsLinearVelocity, AddImpulse, etc. The debug line directly after the movement code works just fine, just… no movement. Any suggestions?
TheCharacter.h
#pragma once
#include "GameFramework/Character.h"
#include "TheCharacter.generated.h"
UCLASS()
class ATheCharacter : public APawn
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() OVERRIDE;
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) OVERRIDE;
//Movement
UFUNCTION() void MoveForward(float Val); //handles moving forward and backwards
UFUNCTION() void MoveRight(float Val); //handles moving right and left
TSubobjectPtr<UCameraComponent> myCamera;
TSubobjectPtr<UStaticMeshComponent> myStaticMesh;
}
TheCharacter.cpp
#include "TEST_fps.h"
#include "TheCharacter.h"
ATheCharacter::ATheCharacter(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
myCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("myCamera"));
myStaticMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("myStaticMesh"));
RootComponent = myStaticMesh;
myCamera->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshObj(TEXT("StaticMesh'/Game/Assets/MyStuff/Geometry/laser100.laser100'"));
myStaticMesh->StaticMesh = StaticMeshObj.Object;
}
void ATheCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
//Debug line shows up
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("TheCharacter: LOADED OK"));
TArray<USceneComponent*, FDefaultAllocator> complist;
RootComponent->GetChildrenComponents(true, complist);
for (int i = 0; i < complist.Num(); i++)
{
//Debug line shows up
GEngine->AddOnScreenDebugMessage(0-i, 4.f, FColor::Yellow, complist[i]->GetName());
}
}
}
void ATheCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
InputComponent->BindAxis("MoveForward", this, &ATheCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &ATheCharacter::MoveRight);
}
void ATheCharacter::MoveForward(float Val)
{
if ((Controller != NULL) && (Val != 0.0f))
{
Cast<UStaticMeshComponent>(RootComponent)->SetPhysicsLinearVelocity(FVector(100.f, 0.0f, 0.0f));
//Debug line shows up
GEngine->AddOnScreenDebugMessage(-5, 1.f, FColor::Yellow, "MOVEFORWARD: SET PHYSICS LINEAR VELOCITY CALLED");
}
}
void ATheCharacter::MoveRight(float Val)
{
if ((Controller != NULL) && (Val != 0.0f))
{
Cast<UStaticMeshComponent>(RootComponent)->SetPhysicsLinearVelocity(FVector(0.0f, 100.0f, 0.0f));
//Debug line shows up
GEngine->AddOnScreenDebugMessage(-5, 1.f, FColor::Yellow, "MOVERIGHT: SET PHYSICS LINEAR VELOCITY CALLED");
}
}
If someone could shed some light on this, I’d really appreciate it. I personally prefer to do all my work through code and C++, so, learning the syntax and how things need to happen in their specific ways with UE4 is a little bit frustrating at times.
Thanks so much guys, and if there is any additional information you need, please just ask and I will provide anything and everything.