ちょうきょう666

导航

UE4c++4来回移动加上倒计时后恢复原来颜色

//h里面的代码
#pragma once

#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "TargetStaticMeshActor.generated.h"

/**
 * 
 */
UCLASS()
class LEAMCPP_API ATargetStaticMeshActor : public AStaticMeshActor
{
    GENERATED_BODY()


    UFUNCTION()
    void NotifyHitCallBack();//Tick调用的函数
    UFUNCTION()
        void TickCallBack();//Tick调用的函数


    FVector Direction;
public:
    //一下两个函数是被击中颜色交替变化
    UPROPERTY(EditDefaultsOnly, Category = "TargetSetting")
        UMaterialInterface* TargetRed;
    UPROPERTY(EditDefaultsOnly, Category = "TargetSetting")
        UMaterialInterface* TBlue;

    UPROPERTY(EditDefaultsOnly, Category = "TargetSetting")
        UMaterialInterface* Tyuan;//倒计时后恢复的颜色

    ATargetStaticMeshActor();
    virtual void NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, 
        class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
        FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override;

    virtual void Tick(float DeltaSeconds) override;//重写Tick方法
};
//CPP里面的代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "TargetStaticMeshActor.h"
#include "Engine/Engine.h"
#include "LeamCppProjectile.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Engine/LatentActionManager.h"
#include "GameFramework/Actor.h"
#include "Kismet/KismetMathLibrary.h"

void ATargetStaticMeshActor::NotifyHitCallBack()//倒计时后自动切换颜色
{
    class UStaticMeshComponent* staticMesh = GetStaticMeshComponent();
    staticMesh->SetMaterial(0, Tyuan);
}


ATargetStaticMeshActor::ATargetStaticMeshActor()
{
    PrimaryActorTick.bCanEverTick = true;//Tick是否被启用
    Direction = FVector(0, -10, 0);//向量初始 化

}
static bool Ifc=true;
void ATargetStaticMeshActor::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
    Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit);
    if (Other == nullptr) return;

    ALeamCppProjectile* projectile = Cast<ALeamCppProjectile>(Other);
    if (projectile != nullptr) {
        class UStaticMeshComponent* staticMesh = GetStaticMeshComponent();
        FLatentActionInfo LatentInfo;
        LatentInfo.Linkage = 0;
        LatentInfo.CallbackTarget = this;//作用对象
        LatentInfo.ExecutionFunction = "NotifyHitCallBack";//绑定函数名称
        LatentInfo.UUID = __LINE__;//行号为ID
        UKismetSystemLibrary::Delay(GetWorld(),0.5f,LatentInfo);//第一个参数是事件位置默认写GetWorld就行,第二个参数是倒计时时间,第三个为FLatentActionInfo变量
        if (Ifc) {
            staticMesh->SetMaterial(0, TargetRed);
            Ifc = false;
        }
        else {
            staticMesh->SetMaterial(0, TBlue);
            Ifc = true;
        }
        
        if (GEngine)
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("ALeamCppProjectile"));
        }
    }
    else {
        if (GEngine)
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotALeamCppProjectile"));
        }
    }
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotifyHit"));
    }


}
void ATargetStaticMeshActor::TickCallBack()
{
    Direction *= -1;//倒计时结束后转向
}

void ATargetStaticMeshActor::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    AddActorLocalOffset(UKismetMathLibrary::Normal(Direction) * DeltaSeconds * 200);//设置本地坐标位移,参数是向量归一化

    FLatentActionInfo LatentInfo;
    LatentInfo.Linkage = 0;
    LatentInfo.CallbackTarget = this;
    LatentInfo.ExecutionFunction = "TickCallBack";
    LatentInfo.UUID = __LINE__;
    UKismetSystemLibrary::Delay(GetWorld(), 3.0f, LatentInfo);
}

 

posted on 2020-11-23 11:20  ちょうきょう666  阅读(291)  评论(0编辑  收藏  举报