Can't Manage to Get Collisions Working C++

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

This is unlikely, but you could double check that the collision on the floor is both set to block all and that it exists in the mesh editor.

You said your own character is already set to block all on everything, so that’s all I can think of.

Yea, as far as I can see all my meshes including the ground have collisions set to BlockAll. Weirdly I’ve dropped in a Sphere to use as a ball and my character cube is able to interact and push that around. The Sphere also collides with the ground/walls I have set up but my character seems to only be colliding with the sphere. Everything else it moves straight through…

open your cube in the editor and look for “Collision Complexity”, set this to “Use complex as simple”

Collisions weren’t set it seems however that hasn’t actually had any effect that I can see. I’ve enabled that setting on other objects also to no effect.

I’ve noticed I can get collisions to work when enabling Simulate Physics However when I do that my character goes mental spinning around especially when it collides with another object

oh i just realized youre using AddActorLocalOffset

this function just moves the actor in space and doesnt handle collision, you will have to do all that manually or use PawnMovementComponent to move your character. Look at UFloatingPawnMovement if you want a simple example