博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最近想写一个类似鬼泣 收集红魂的功能,陆续写点东西作为笔记
阅读量:5955 次
发布时间:2019-06-19

本文共 4530 字,大约阅读时间需要 15 分钟。

在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 valuesAPickup::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
(TEXT("PickupMesh")); RootComponent = PickupMesh; bIsActive = true;}// Called when the game starts or when spawnedvoid APickup::BeginPlay(){ Super::BeginPlay(); }// Called every framevoid 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
(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

你可能感兴趣的文章
Openstack M版快速配置(二)--刷数据库
查看>>
sed
查看>>
Failed to execute goal org.apache.maven.plugins:ma
查看>>
如何利用华为交换机配置SSH登录
查看>>
数据挖掘的产品
查看>>
2013.8.4thinkPHp学习
查看>>
cygwin图文安装教程
查看>>
去小机化思维(二)--【软件和信息服务】2015.03
查看>>
华为交换机的端口hybrid端口属性配置
查看>>
算法学习之归并排序(java实现)
查看>>
css3在线快速制作工具
查看>>
linux系统进入救援模式
查看>>
oracle常用网址
查看>>
我的友情链接
查看>>
10个SQL注入工具
查看>>
[李景山php]每天laravel-20160826|EncryptionServiceProvider
查看>>
[李景山php]每天laravel-20161005|Validator.php-5
查看>>
php读取大文件详解【OK】
查看>>
Gnome 快捷键汇总
查看>>
Android基础知识点的整理3
查看>>