最近想写一个类似鬼泣 收集红魂的功能,陆续写点东西作为笔记

在C++中使用OnComponentBeginOverlap事件

http://www.cnblogs.com/blueroses/p/5187236.html

写了2个类,因为一开始没有设计好,导致有很多无用代码,而且代码有相当大的修改余地,反正最终结果满足要求,贴下代码仅当抛砖引玉

这里的收集代码写在角色类,通过碰撞事件修改SoulPickup类中的bMoveToCharactor布尔值,来达到移动的目的。如何使用碰撞可以看一下下面的代码

http://www.cnblogs.com/blueroses/p/5187236.html

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

#pragma once

#include "GameFramework/Actor.h"
#include "Pickup.generated.h"

UCLASS()
class THIRDPERSONPLUGIN_API APickup : public AActor
{
    GENERATED_BODY()
    
public:    
    // Sets default values for this actor's properties
    APickup();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    
    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

    UFUNCTION(BlueprintPure, Category = "PickUp")
    bool IsActive();

    UFUNCTION(BlueprintCallable, Category = "PickUp")
    void SetActive(bool bActive);

    //强制内联函数,返回Mesh指针
    FORCEINLINE class UStaticMeshComponent *GetMesh() const { return PickupMesh; }

    UFUNCTION(BlueprintNativeEvent)
    void WasCollected();

    virtual void WasCollected_Implementation();
protected:
    bool bIsActive;
private:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
    class UStaticMeshComponent *PickupMesh;
};
// Fill out your copyright notice in the Description page of Project Settings.

#include "ThirdPersonPlugin.h"
#include "Pickup.h"


// Sets default values
APickup::APickup()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = false;

    PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
    RootComponent = PickupMesh;

    bIsActive = true;
}

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

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

}
////设置是否激活
void APickup::SetActive(bool bActive)
{
    bIsActive = bActive;
}
////查看Pickup是否处于激活状态
bool APickup::IsActive()
{
    return bIsActive;
}
void APickup::WasCollected_Implementation()
{
    bIsActive = false;
}
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Pickup.h"
#include "SoulPickup.generated.h"

/**
 * 
 */
UCLASS()
class THIRDPERSONPLUGIN_API ASoulPickup : public APickup
{
    GENERATED_BODY()
public:
    virtual void BeginPlay() override;

    virtual void Tick(float DeltaSeconds) override;
    
    virtual void WasCollected_Implementation() override;
    
    ASoulPickup();

    UFUNCTION(BlueprintCallable, Category = "SoulPickup")
    void MoveToCharactor(bool bMove);
protected:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoulPickup")
    bool bMoveToCharactor;
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoulPickup")
    FVector charactorLocation;
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoulPickup")
    FVector currentLocation;
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoulPickup")
    FVector direction;

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "SoulPickup")
    float moveSpeed;

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

#include "ThirdPersonPlugin.h"
#include "SoulPickup.h"
#include "ThirdPersonPluginCharacter.h"
#include "Kismet/GameplayStatics.h"

ASoulPickup::ASoulPickup()
{
    moveSpeed = 2;
    PrimaryActorTick.bCanEverTick = true;
}

void ASoulPickup::BeginPlay()
{
    Super::BeginPlay();
}

void ASoulPickup::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    if (bMoveToCharactor)
    {
        AThirdPersonPluginCharacter *MyCharacter = Cast<AThirdPersonPluginCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
        if (MyCharacter)
        {
            //取得角色坐标,与当前自己的位置
            charactorLocation=MyCharacter->GetActorLocation();
            currentLocation = GetActorLocation();
            //取得方向向量并且规范化
            direction=charactorLocation - currentLocation;
            direction.Normalize();
            //算出移动距离并且移动
            currentLocation += moveSpeed*direction;
            SetActorLocation(currentLocation);
        }else
        {
            return;
        }
        ////到达位置就销毁
        //if (currentLocation==charactorLocation)
        //{
        //    Destroy();
        //}
    }
}

void ASoulPickup::WasCollected_Implementation()
{
    
}

void ASoulPickup::MoveToCharactor(bool bMove)
{
    //设置是否移动到角色
    
    bMoveToCharactor = bMove;
}
原文地址:https://www.cnblogs.com/blueroses/p/5187174.html