Hi I am trying to achieve when a simple character who when presses a button within a trigger opens a door.
I am trying to use the character capsule component which for the collision with trigger. But i cannot seem to fire the Overlap function call. Although the same is happening in blueprints.
Here’s My Code
MyCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
UCLASS()
class DARKHORSEUNREAL_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
/** First person camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FirstPersonCameraComponent;
/** Base turn rate*/
UPROPERTY(VisibleAnywhere, Category = "Camera")
float BaseTurnRate;
/** Base look up/down rate */
UPROPERTY(VisibleAnywhere, Category = "Camera")
float BaseLookUpRate;
//Creating a variable to store and call the capsule component from ACharacter class
UPROPERTY(VisibleAnywhere, Category = “Capsule”)
class UCapsuleComponent* CollisionCapsule;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Handles moving forward/backward
void MoveForward(float Val);
//Handles stafing movement, left and right
void MoveRight(float Val);
//Handles the input for door closing/opening
void DoorAction();
public:
// Constructor-Sets default values for this character's properties
AMyCharacter();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION()
virtual void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
MyCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "Engine/World.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
// 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;
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FPPCamera")); //Assigning the camera
FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
FirstPersonCameraComponent->RelativeLocation = FVector(-39.56f, 1.75f, 64.f); // Position the camera
FirstPersonCameraComponent->bUsePawnControlRotation = true;
CollisionCapsule = GetCapsuleComponent();
CollisionCapsule->OnComponentBeginOverlap.AddDynamic(this, &AMyCharacter::OnOverlapBegin);
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
//Bind the DoorAction Event
PlayerInputComponent->BindAction("DoorAction", IE_Pressed, this, &AMyCharacter::DoorAction);
// Bind jump events
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
// Bind movement events
PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
//Binding The Mouse Axis events
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
void AMyCharacter::MoveForward(float Val)
{
if (Val != 0.0f)
{
AddMovementInput(GetActorForwardVector(), Val);
}
}
void AMyCharacter::MoveRight(float Val)
{
if (Val != 0.0f)
{
AddMovementInput(GetActorRightVector(), Val);
}
}
void AMyCharacter::DoorAction()
{
UE_LOG(LogTemp, Warning, TEXT("E Pressed"));
}
void AMyCharacter::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Overlap Begins"));
}