UE4 C++ 一个Character踩地雷


// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyinPlayer.generated.h"UCLASS()
class GETSTARTED_API AMyinPlayer : public ACharacter
{GENERATED_BODY()public:// Sets default values for this character's propertiesAMyinPlayer();UPROPERTY(VisibleAnywhere,BlueprintReadOnly)class USpringArmComponent* SpringArm;UPROPERTY(VisibleAnywhere, BlueprintReadOnly)class UCameraComponent* FollowCamera;float BaseTurnRate;float BaseTurnUpRate;UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player Stats")float MaxHealth;//生命值UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Stats")float Health;UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player Stats")float MaxStamina;//耐力UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Stats")float Stamina;UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Stats")int32 Coins;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:  // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;virtual void Jump() override;//重写的一个函数void MoveForward(float value);void MoveRight(float value);void Turn(float value);void LookUp(float value);void TurnAtRate(float value);void LookUpAtRate(float value);void IncreaseHealth(float value);void IncreaseStamina(float value);void IncreaseCoin(int value);virtual float TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;
};

这里就是给角色加了一个事件任意伤害

// Fill out your copyright notice in the Description page of Project Settings.#include "Characters/Player/MyinPlayer.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AMyinPlayer::AMyinPlayer()
{// 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;SpringArm=CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));SpringArm->SetupAttachment(GetRootComponent());SpringArm->TargetArmLength = 600.0f;SpringArm->bUsePawnControlRotation = true;FollowCamera=CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);FollowCamera->bUsePawnControlRotation = false;GetCapsuleComponent()->SetCapsuleSize(25.0f, 100.0f);//设置胶囊体组件的高和宽bUseControllerRotationYaw = false;GetCharacterMovement()->bOrientRotationToMovement = true;//继承角色移动类里面的“将旋转朝向远动”设置为GetCharacterMovement()->RotationRate=FRotator(0.0f, 500.0f, 0.0f);//继上面“旋转速率”BaseTurnRate = 65.0f;BaseTurnUpRate = 65.0f;GetCharacterMovement()->JumpZVelocity = 500.0f;//跳的时候一个速度Z轴的速度GetCharacterMovement()->AirControl = 0.15f;//这是在空中的移动速度MaxHealth = 100.0f;Health = MaxHealth;MaxStamina = 150.f;Stamina = MaxStamina;Coins = 0;
}// Called when the game starts or when spawned
void AMyinPlayer::BeginPlay()
{Super::BeginPlay();}// Called every frame
void AMyinPlayer::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}// Called to bind functionality to input
void AMyinPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);check(PlayerInputComponent);//是否有效,可用性的检查PlayerInputComponent->BindAxis("MoveForward", this, &AMyinPlayer::MoveForward);//第一个是轴映射的名称PlayerInputComponent->BindAxis("MoveRight", this, &AMyinPlayer::MoveRight);PlayerInputComponent->BindAxis("Turn", this, &AMyinPlayer::Turn);PlayerInputComponent->BindAxis("LookUp", this, &AMyinPlayer::LookUp);PlayerInputComponent->BindAxis("TurnAtRate", this, &AMyinPlayer::TurnAtRate);PlayerInputComponent->BindAxis("LookUpAtRate", this, &AMyinPlayer::LookUpAtRate);//轴PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyinPlayer::Jump);//按键PlayerInputComponent->BindAction("Jump", IE_Released, this, &AMyinPlayer::StopJumping);//落下}void AMyinPlayer::Jump()//跳跃
{Super::Jump();}void AMyinPlayer::MoveForward(float value)
{//if ((Controller != nullptr) && (value != 0.0f)) {//FRotator Rotation = Controller->GetControlRotation();//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);//AddMovementInput(Direction, value);//}AddMovementInput(FollowCamera->GetForwardVector(), value);//添加移动输入
}void AMyinPlayer::MoveRight(float value)
{//if ((Controller != nullptr) && (value != 0.0f)) {//FRotator Rotation = Controller->GetControlRotation();//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);//AddMovementInput(Direction, value);//}AddMovementInput(FollowCamera->GetRightVector(), value);
}void AMyinPlayer::Turn(float value)
{if (value != 0.0f){AddControllerYawInput(value);}
}void AMyinPlayer::LookUp(float value)
{if (GetControlRotation().Pitch < 270.0f && GetControlRotation().Pitch>180.0f && value > 0.0f){return;}if (GetControlRotation().Pitch > 45.0f && GetControlRotation().Pitch < 180.0f && value < 0.0f) {return;}AddControllerPitchInput(value);
}void AMyinPlayer::TurnAtRate(float value)
{float valuee = value * BaseTurnRate*GetWorld()->GetDeltaSeconds();//乘以一个Time函数里的DeltaTime,作用120帧和30帧的电脑跑起来一样快if (valuee != 0.0f){AddControllerYawInput(value);}
}void AMyinPlayer::LookUpAtRate(float value)
{float valuee = value * BaseTurnUpRate * GetWorld()->GetDeltaSeconds();//乘以一个Time函数里的DeltaTime,作用120帧和30帧的电脑跑起来一样快if (GetControlRotation().Pitch < 270.0f && GetControlRotation().Pitch>180.0f && value > 0.0f){return;}if (GetControlRotation().Pitch > 45.0f && GetControlRotation().Pitch < 180.0f && value < 0.0f) {return;}AddControllerPitchInput(valuee);
}void AMyinPlayer::IncreaseStamina(float value)
{Health = FMath::Clamp(Health + value, 0.0f, MaxHealth);
}void AMyinPlayer::IncreaseCoin(int value)
{Stamina = FMath::Clamp(Stamina + value, 0.0f, MaxStamina);
}//事件任意伤害
float AMyinPlayer::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{if (Health - Damage <= 0.0f) {Health = FMath::Clamp(Health - Damage, 0.0f, MaxHealth);}else {Health -= Damage;}return Health;
}void AMyinPlayer::IncreaseHealth(float value)
{Coins += value;
}

炸弹:

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Gameplay/Interactableitem.h"
#include "Explosiveitem.generated.h"/*** */
UCLASS()
class GETSTARTED_API AExplosiveitem : public AInteractableitem
{GENERATED_BODY()public :AExplosiveitem();UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")float Damage;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")TSubclassOf<UDamageType> DamageTypeClass;public :virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;virtual    void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;};
// Fill out your copyright notice in the Description page of Project Settings.#include "Gameplay/Explosiveitem.h"
#include "Characters/Player/MyinPlayer.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Components/SphereComponent.h"
AExplosiveitem::AExplosiveitem() {Damage = 20.0f;TriggerVolume->SetSphereRadius(50.0f);
}
void AExplosiveitem::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);if (OtherActor) {const AMyinPlayer* MainPlayer = Cast<AMyinPlayer>(OtherActor);if (MainPlayer) {if (OverlapParticle) {UGameplayStatics::SpawnEmitterAtLocation(this, OverlapParticle, GetActorLocation(), FRotator(0.0f),true);//生成爆炸的位置}if (OverlapSound) {UGameplayStatics::PlaySound2D(this,OverlapSound);//播放声音}UGameplayStatics::ApplyDamage(OtherActor,Damage,nullptr,this,DamageTypeClass);//这个就是应用伤害Destroy();//销毁自己}}
}void AExplosiveitem::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{Super::OnOverlapEnd(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
}

炸弹继承这个类:

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Interactableitem.generated.h"UCLASS()
class GETSTARTED_API AInteractableitem : public AActor
{GENERATED_BODY()public:    // Sets default values for this actor's propertiesAInteractableitem();UPROPERTY(VisibleAnywhere, BlueprintReadWrite)class USphereComponent* TriggerVolume;UPROPERTY(VisibleAnywhere, BlueprintReadWrite)class UStaticMeshComponent* DisplayMesh;UPROPERTY(VisibleAnywhere, BlueprintReadWrite)class UParticleSystemComponent* IdleParticleComponent;//粒子系统的组件UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Interactable Item|Particles")class UParticleSystem* OverlapParticle;//重叠播放的粒子,可以看做一个资源UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Interactable Item|Sounds")class USoundCue* OverlapSound;//一个声音的资源吧UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Interactable Item|Item Properties")bool bNeedRotate;//需要旋转吗UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Interactable Item|Item Properties")float RotationRate;//旋转的速率protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;UFUNCTION()virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);UFUNCTION()virtual  void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);};
// Fill out your copyright notice in the Description page of Project Settings.#include "Gameplay/Interactableitem.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
#include "particles/ParticleSystemComponent.h"// Sets default values
AInteractableitem::AInteractableitem()
{// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;TriggerVolume = CreateDefaultSubobject<USphereComponent>(TEXT("TriggerVolume"));RootComponent = TriggerVolume;// 组件 ->SetCollisionEnabled(ECollisionEnabled::NoCollision);     注释:没有碰撞// 组件 ->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);     注释:只有物理// 组件 ->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); 注释:查询和物理// 组件 ->SetCollisionEnabled(ECollisionEnabled::QueryOnly);       注释:只有查询// 组件 ->SetCollisionEnabled(ECollisionEnabled::Type);             注释:目前理解为自定义TriggerVolume->SetCollisionEnabled(ECollisionEnabled::QueryOnly);//仅查询TriggerVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);//设为世界静态TriggerVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//让所有频道都忽略TriggerVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);//再单独开启一个Pawn 频道的一个事件DisplayMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DisplayMesh"));DisplayMesh->SetupAttachment(GetRootComponent());IdleParticleComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("IdleParticleComponent"));IdleParticleComponent->SetupAttachment(GetRootComponent());bNeedRotate = true;RotationRate = 45.0f;
}// Called when the game starts or when spawned
void AInteractableitem::BeginPlay()
{Super::BeginPlay();TriggerVolume->OnComponentBeginOverlap.AddDynamic(this,&AInteractableitem::OnOverlapBegin);TriggerVolume->OnComponentEndOverlap.AddDynamic(this, &AInteractableitem::OnOverlapEnd);}// Called every frame
void AInteractableitem::Tick(float DeltaTime)
{Super::Tick(DeltaTime);if (bNeedRotate) {FRotator NewRotation = GetActorRotation();NewRotation.Yaw += RotationRate * DeltaTime;SetActorRotation(NewRotation);}}void AInteractableitem::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{}void AInteractableitem::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{}

血条绑定就是简单的蓝图绑定了!!!!

UE4 C++ 一个Character踩地雷相关推荐

  1. 使用UE4创建一个简单真实的地球(一)

    使用UE4创建一个简单真实的地球 准备地球模型Mesh 三角面片:520191 顶点:261067 准备纹理贴图 可以在这里下载地球的纹理贴图:包括白天.夜晚.法线贴图以及云图等. https://w ...

  2. 使用ue4做一个可以显示任意数字变化的计数器方法

    使用ue4做一个可以显示任意数字变化过程的计数器 功能描述: 该功能可以任意输入一个数值然后通过UI绑定数值的方式来\显示数值变化的过程,例如可以显示从0变到100 或者从100变到0,同时还支持调节 ...

  3. firefox os 开发踩地雷游戏源码

    踩地雷这游戏在大家生活中应该不陌生吧!咋们八零九零后都基本上玩过这款游戏,这也曾是windows xp上一款标配的单机游戏,想想无聊的时候拿出来玩玩倒是不错,今天推出Firefox os版踩地雷源码, ...

  4. 使用UE4创建一个简单真实的地球(二)

    使用UE4创建一个简单真实的地球 如何创建一个简单的地球材质. BaseColor 基础颜色 排除由反射引起的杂光之后物体的颜色.主要用来模拟地球的真实表面. 白昼 地图与云图叠加,即图像的叠加运算( ...

  5. UE4 3dsmax 一个电脑屏幕模型的demo

    目的是想用3dsmax建一个电脑模型,让其在UE4拥有两个可赋予的材质接口,如下图: 一个材质用来作为屏幕的材质,另一个作为电脑外壳的材质 步骤: 1.在3dsmax建立一个电脑模型,本人并不擅长建模 ...

  6. (七)使用jedis连接单机和集群(一步一个坑踩出来的辛酸泪)

    环境准备: redis-4.0.9,最新版了 ruby:redis-x.x.x.gem    这个gem什么版本都行,我redis4用3.0.0的gem正常跑 jedis-2.9.0.jar,最新版 ...

  7. WPF 记一个Popup踩坑记录

    看名字就知道,它是一个弹出控件,顾名思义,我们可以用它来实现类似Combobox那种,点击后弹出下面选项列表的操作. 记录: 需求:有一个文本框 ,鼠标点击后,弹出一个Popup. 我编写了以下xam ...

  8. 记录上一个项目踩过的坑

    1.objecthtmldivelement对象 var avc = document.getElementById("div1"); alert(avc ); 为objectht ...

  9. ue4创建一个游戏模式Game mode

    因为我们是要控制球,所以需要创建一个新的游戏模式,这个游戏模式是和系统自带的游戏模式是有区别的,是我们球的游戏模式. 选择放置的文件夹gamemode,进行命名BallGameMode 在默认Pawn ...

最新文章

  1. GitHub上传代码、更新代码、token设置
  2. graphviz linux教程,程序员绘图利器 — Graphviz
  3. 整合shiro出现UnsatisfiedDependencyException,org.springframework.beans.factory.BeanNotOfRequiredTypeExcep
  4. python常用面试题_史上最全Python工程师常见面试题集锦,有这一份就够了
  5. java数据类型怎样理解_深入理解Java之数据类型
  6. Freemarker自定义标签
  7. tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验
  8. 安川机器人报错代码_今日 IPO|对标库卡机器人的先惠技术上市 近八成收入靠上汽...
  9. Jeecg-Uniapp 移动框架开发环境搭建—— APP解决方案
  10. Hibernate 1
  11. 2018年全球智能手机销售收入增至5220亿美元 但销量却下降了
  12. 9080端口对应服务器文件位置,Filenet更改端口-更改9080 端口到 80 端口
  13. 【P2P网络】BitTorrent协议中文版4
  14. Unsupervised Domain Adaptation by Backpropagation
  15. 设计稿 自动html,简单的登陆页面PSD设计稿来演示转化为HTML页面的全部过程
  16. 古时候有个【百僧问题】,一百馒头一百僧,大僧三个更无争,小僧三人分一个,大小和尚各几丁? *...
  17. 恒源云(GPUSHARE)_未闻Prompt名(论文学习笔记)
  18. 爬虫 使用python+requests模块爬取12306网站的车次信息
  19. hmm念什么_HMM是什么意思
  20. Android Dialer,Mms,Contacts源码修改笔记,移动端混合开发经验

热门文章

  1. 实验室网页以及后台管理系统(1)-表格设计和建表语句
  2. 利用微信公众号搭建天气查询
  3. 系统程序员成长计划-像机器一样思考(二)
  4. 321酷生活导航第一期:AIDN(js和flash类的小游戏)
  5. 全网最全java开发环境下载-jdk-tomcat-maven-eclips-idea
  6. RK3588平台开发系列讲解(内核调试篇)CPU Hotplug 调试
  7. Java 核心类库面试题
  8. 江苏专转本上岸后还能不能换专业
  9. 快速云:利用废旧安卓手机改造linux服务器
  10. Android 系统级APP 升级方案 OTA全流程