UE4 actor碰到炸弹血条减少

当actor在碰到炸弹的时候,血条就会减少;碰到金币的时候,金币的数量就会加1.编辑HUD界面
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Item.h"
#include "Explosive.generated.h"

/**
 *
 */
UCLASS()
class FIRSTPROJECT_API AExplosive : public AItem {
	GENERATED_BODY()

public:
	AExplosive();

	UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category="Damage")
	float damage = 10.0;


	//UFUNCTION() 在子类中不需要添加这个宏
	void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;

	//UFUNCTION()
	void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "Explosive.h"
#include "MainCharacter.h"

AExplosive::AExplosive() {

}

void AExplosive::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
	Super::onBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
	UE_LOG(LogTemp, Warning, TEXT("AExplosive::onBeginOverlap"));

	if (OtherActor){
		AMainCharacter* mainCharactor = Cast<AMainCharacter>(OtherActor);
		if (mainCharactor){
			mainCharactor->decrementhHealth(damage);
		}
	}
}

void AExplosive::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
	Super::onEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
	UE_LOG(LogTemp, Warning, TEXT("AExplosive::onEndOverlap"));
}

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Item.h"
#include "Pickup.generated.h"

/**
 * 
 */
UCLASS()
class FIRSTPROJECT_API APickup : public AItem
{
	GENERATED_BODY()

public:
	APickup();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Coins")
	int coinsCount = 10;

	//UFUNCTION() 在子类中不需要添加这个宏
	void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;

	//UFUNCTION()
	void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
	
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "Pickup.h"
#include "MainCharacter.h"

APickup::APickup() {

}

void APickup::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
	Super::onBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
	UE_LOG(LogTemp, Warning, TEXT("APickup::onBeginOverlap"));

	if (OtherActor) {
		AMainCharacter* mainCharactor = Cast<AMainCharacter>(OtherActor);
		if (mainCharactor) {
			mainCharactor->addCoinsCount(coinsCount);
		}
	}

}

void APickup::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
	Super::onEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
	UE_LOG(LogTemp, Warning, TEXT("APickup::onEndOverlap"));
}
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MainCharacter.generated.h"

UENUM()
enum class EMovementStatus :uint8 {
	EMS_Normal UMETA(DisplayName = "Normal"),
	EMS_Sprinting UMETA(DisplayName = "Sprinting"),
	EMS_Max UMETA(DisplayName = "Max"),
};

UCLASS()
class FIRSTPROJECT_API AMainCharacter : public ACharacter {
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMainCharacter();

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category="Eunms")
	EMovementStatus movementStatus;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* cameraSpringArm;
	FORCEINLINE class USpringArmComponent* getCameraSpringArm() {
		return cameraSpringArm;
	}

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* followCamera;
	FORCEINLINE UCameraComponent* getFollowCamera() {
		return followCamera;
	}

	//使用方向键的转动速率
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	float baseTurnRate;//度每秒
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	float baseLookupRate;

	//角色属性值
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="PlayerState")
	float maxHeath = 100.0;   //最大健康值

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PlayerState")
	float health = 65.0;     //当前健康值

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "PlayerState")
	float maxStamina = 350.0; //最大耐力值

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PlayerState")
	float stamina = 120.0;    //当前耐力值

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PlayerState")
	int32 coinsCount = 0.0; //金币的数量

	void decrementhHealth(float v);
	void die();

	void addCoinsCount(int v);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

private:
	void moveForward(float value);
	void moveRight(float value);
	void turnAtRate(float rate);
	void lookUpAtRate(float tate);
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "MainCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"

// Sets default values
AMainCharacter::AMainCharacter() {
	// 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;

	cameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	cameraSpringArm->SetupAttachment(GetRootComponent());
	cameraSpringArm->TargetArmLength = 400.0;
	cameraSpringArm->bUsePawnControlRotation = true; //摇臂跟着控制器的旋转

	//设置碰撞胶囊的大小
	GetCapsuleComponent()->SetCapsuleSize(48, 105.0);

	followCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	followCamera->SetupAttachment(cameraSpringArm, USpringArmComponent::SocketName);
	followCamera->bUsePawnControlRotation = false; //摄像机不跟着摇臂旋转

	//当摄像机旋转的时候,角色不会旋转,仅对摄像机有影响 (上下左右按键)
	bUseControllerRotationYaw = false;

	//将选装绑定到角色上,而不是绑定到控制器上
	GetCharacterMovement()->bOrientRotationToMovement = true;//角色沿着基于输入的方向移动
	GetCharacterMovement()->RotationRate = FRotator(0.0, 540.0, 0.0);
	GetCharacterMovement()->JumpZVelocity = 650.0;//向上跳起的高度和速度
	GetCharacterMovement()->AirControl = 0.2;//当角色跳起的时候,可以在空中进行控制,如果不想被控制的话,可以设置一个比较小的值
}

void AMainCharacter::decrementhHealth(float v) {
	health -= v;
	if (health<= 0.0f) {
		health = 0.0;
		die();
	}
}

void AMainCharacter::die() {

}

void AMainCharacter::addCoinsCount(int v) {
	coinsCount += v;
}

// Called when the game starts or when spawned
void AMainCharacter::BeginPlay() {
	Super::BeginPlay();

}

// Called every frame
void AMainCharacter::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	check(PlayerInputComponent);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAxis("MoveForward", this, &AMainCharacter::moveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AMainCharacter::moveRight);

	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("Lookup", this, &APawn::AddControllerPitchInput);

	PlayerInputComponent->BindAxis("TurnRate", this, &AMainCharacter::turnAtRate);
	PlayerInputComponent->BindAxis("LookupRate", this, &AMainCharacter::lookUpAtRate);

}

void AMainCharacter::moveForward(float value) {
	if ((Controller!=nullptr) && (value != 0.0)){
		FRotator rotation = Controller->GetControlRotation();
		FRotator yawRotation(0.0, rotation.Yaw, 0.0);

		FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::X);

		AddMovementInput(direction, value);
	}
}

void AMainCharacter::moveRight(float value) {
	if ((Controller != nullptr) && (value != 0.0)) {
		FRotator rotation = Controller->GetControlRotation();
		FRotator yawRotation(0.0, rotation.Yaw, 0.0);

		FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::Y);

		AddMovementInput(direction, value);
	}
}

void AMainCharacter::turnAtRate(float rate) {
	AddControllerYawInput(rate * baseTurnRate * GetWorld()->GetDeltaSeconds());
}

void AMainCharacter::lookUpAtRate(float rate) {
	AddControllerPitchInput(rate * baseLookupRate * GetWorld()->GetDeltaSeconds());
}


aaa

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值