Hey,
I’m testing out UE5 with C++ for the first time but I can’t manage to get collision working for my Character Cube. I’ve set collisions to BlockAll but I’m still going through the floor. I’ll add the current code I’m using here.
- mvt_Character.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Game/mvt_Character.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
// Sets default values
Amvt_Character::Amvt_Character()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>("MeshComp");
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>("SpringArmComp");
CameraComp = CreateDefaultSubobject<UCameraComponent>("CameraComp");
RootComponent = MeshComp;
SpringArmComp->SetupAttachment(MeshComp);
CameraComp->SetupAttachment(SpringArmComp);
}
// Called when the game starts or when spawned
void Amvt_Character::BeginPlay()
{
Super::BeginPlay();
}
// Called to bind functionality to input
void Amvt_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAxis("MoveForward", this, &Amvt_Character::MoveForward);
InputComponent->BindAxis("MoveRight", this, &Amvt_Character::MoveRight);
InputComponent->BindAxis("MoveUp", this, &Amvt_Character::MoveUp);
}
void Amvt_Character::MoveForward(float Value)
{
if ((Controller) && (Value != 0.0f)) {
const float TargetThrustSpeed = Value * MoveForce;
CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrustSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
const FVector LocalMove = FVector(CurrentThrust * GetWorld()->GetDeltaSeconds(), 0.f, 0.f);
AddActorLocalOffset(LocalMove);
}
if ((Controller) && (Value == 0.0f)) {
const float TargetThrustSpeed = 0.0f;
CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrustSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
const FVector LocalMove = FVector(CurrentThrust * GetWorld()->GetDeltaSeconds(), 0.f, 0.f);
AddActorLocalOffset(LocalMove);
}
}
void Amvt_Character::MoveRight(float Value)
{
const FRotator Right = FRotator(0, Value, 0);
AddActorLocalRotation(Right);
}
void Amvt_Character::MoveUp(float Value)
{
if ((Controller) && (Value != 0.0f)) {
const float TargetLiftSpeed = Value * MoveForce;
CurrentLift = FMath::FInterpTo(CurrentLift, TargetLiftSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
const FVector LocalLift = FVector(0.f, 0.f, CurrentLift * GetWorld()->GetDeltaSeconds());
AddActorLocalOffset(LocalLift);
}
if ((Controller) && (Value == 0.0f)) {
const float TargetLiftSpeed = 0.0f;
CurrentLift = FMath::FInterpTo(CurrentLift, TargetLiftSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
const FVector LocalLift = FVector(0.f, 0.f, CurrentLift * GetWorld()->GetDeltaSeconds());
AddActorLocalOffset(LocalLift);
}
}
void Amvt_Character::TurnRight(float Value)
{
}
- mvt_Character.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "mvt_Character.generated.h"
class USpringArmComponent;
class UCameraComponent;
UCLASS()
class MOVETEST_API Amvt_Character : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
Amvt_Character();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component")
UStaticMeshComponent* MeshComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component")
USpringArmComponent* SpringArmComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component")
UCameraComponent* CameraComp;
// Variables //
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float MoveForce = 1000.0f;
float CurrentThrust;
float CurrentLift;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
void MoveForward(float Value);
void MoveRight(float Value);
void MoveUp(float Value);
void TurnRight(float Value);
};
I’m sure I’m missing an obvious step somewhere but the docs aren’t particularly helpful so not sure what to do. Been stuck on this hurdle for a while now.
Thanks,
Mitchell