zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    UBI系统原理分析【转】
    udhcpc命令【转】
    Device Tree(三):代码分析【转】
    Linux 中断详解 【转】
    ROS探索总结(三)——ROS新手教程【转】
    机器人操作系统(ROS)教程4:ROS的框架【转】
    你知道市面上机器人都用哪些操作系统吗【转】
    dump_stack调用过程【原创】
    #if、#ifdef、#if defined之间的区别【转】
    浅析firmware完整生存和使用流程 【转】
  • 原文地址:https://www.cnblogs.com/tilyougogannbare666/p/14023452.html
Copyright © 2011-2022 走看看