UE4学习-添加机关并添加代码控制
文章目錄
- 添加機關
- 代碼編寫
- 給密室添加屋頂
- 打印日志
- 控制系統角色
- 創建一個新游戲模式替換DefaultPawn
- 添加抓取組件
- 獲取起點和終點
- 物體拾取,碰撞屬性設置
- 今日完整代碼
添加機關
首先向場景里面添加一個聚光源
添加聚光源以后,可以對其屬性進行修改,如圖:
然后需要給聚光源添加一個觸發體積(TriggerVolume)。
然后調整觸發體積的大小,按空格進行切換模式,移動旋轉縮放
代碼編寫
所有引入的頭文件都需要放在 #include “openDoor.generated.h” 之前。
定義ATriggerVolume的指針,添加UPROPERTY(EditAnywhere),這樣表示可以在任意地方賦值。
所以可以在界面上進行賦值。
賦值如圖:
選中門,然后選中自定義的組件openDoor
編譯完成,會添加Trigger
然后選中TriggerVolume即可。
劃重點:
pawn = GetWorld()->GetFirstPlayerController()->GetPawn(); // 得到defaultPawn
這句代碼不能放到構造函數里面,否則編譯會造成虛幻的崩潰。
給密室添加屋頂
這里使用玻璃作為屋頂。
添加玻璃,然后使用縮放工具進行調整,蓋滿整個房間即可。
打印日志
UE4里面日志,使用UE_LOG,第一個參數默認是LogTemp,第二個參數是日志類型,警告是Warning,字符串使用TEXT() 括起來。
UE_LOG(LogTemp, Warning, TEXT("%s begin openDoor()"), *owner->GetName());
控制系統角色
運行,然后F8彈出,能看到一個DefaultPawn,這個是系統默認角色。
創建一個新游戲模式替換DefaultPawn
創建以后,指定pawn為剛才創建的對象。
運行,F8,pawn就是我們自定義的pawn了。
添加抓取組件
獲取起點和終點
FVector start;FRotator viewRot;GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(start, viewRot);FVector end = start + viewRot.Vector() * 100; // 100cmDrawDebugLine(GetWorld(), start, end, FColor(255, 0, 0), false, 0.0f, 0, 10.0f);物體拾取,碰撞屬性設置
在檢查碰撞的時候,只檢測ECollisionChannel::ECC_PhysicsBody類別的物體。
設置物體屬性:
今日完整代碼
openDoor.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "openDoor.generated.h"class AActor; class ATriggerVolume; UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class MYPROJECT_API UopenDoor : public UActorComponent {GENERATED_BODY()public: // Sets default values for this component's propertiesUopenDoor();protected:// Called when the game startsvirtual void BeginPlay() override;public: // Called every framevirtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;void openDoor();void closeDoor();private:AActor* owner;AActor* pawn;UPROPERTY(EditAnywhere)ATriggerVolume* trigger;float openTime = 0.4f;float lastTime = 0.0f; };openDoor.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "openDoor.h" #include <GameFramework/Actor.h> #include <Engine/TriggerVolume.h> #include <Engine/World.h>// Sets default values for this component's properties UopenDoor::UopenDoor() {// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features// off to improve performance if you don't need them.PrimaryComponentTick.bCanEverTick = true; }// Called when the game starts void UopenDoor::BeginPlay() {Super::BeginPlay();/* openDoor();*/owner = GetOwner();pawn = GetWorld()->GetFirstPlayerController()->GetPawn(); // 得到defaultPawnUE_LOG(LogTemp, Warning, TEXT("%s begin openDoor()"), *owner->GetName()); }// Called every frame void UopenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {Super::TickComponent(DeltaTime, TickType, ThisTickFunction);if (nullptr != trigger && trigger->IsOverlappingActor(pawn)){openDoor();lastTime = GetWorld()->GetTimeSeconds();}if (GetWorld()->GetTimeSeconds() - lastTime > openTime){closeDoor();} }void UopenDoor::openDoor() {// pitch=y yaw=z roll=x /* FRotator newRotator = FRotator(0, 0, 0);*/FRotator newRotator = FRotator(0, 180, 0);owner->SetActorRotation(newRotator); }void UopenDoor::closeDoor() {// pitch=y yaw=z roll=xFRotator newRotator = FRotator(0, 90, 0);owner->SetActorRotation(newRotator); }grabber.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "grabber.generated.h"UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class MYPROJECT_API Ugrabber : public UActorComponent {GENERATED_BODY()public: // Sets default values for this component's propertiesUgrabber();protected:// Called when the game startsvirtual void BeginPlay() override;public: // Called every framevirtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;private:FVector getLineStart();FVector getLineEnd();AActor* lineTrace(); };grabber.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "grabber.h" #include <Engine/World.h> #include <DrawDebugHelpers.h>// Sets default values for this component's properties Ugrabber::Ugrabber() {// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features// off to improve performance if you don't need them.PrimaryComponentTick.bCanEverTick = true;// ... }// Called when the game starts void Ugrabber::BeginPlay() {Super::BeginPlay();// ...}// Called every frame void Ugrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {Super::TickComponent(DeltaTime, TickType, ThisTickFunction); }FVector Ugrabber::getLineStart() {// 設置起點和終點,畫射線FVector start;FRotator viewRot;GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(start, viewRot);return start; }FVector Ugrabber::getLineEnd() {FVector start;FRotator viewRot;GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(start, viewRot);return start + viewRot.Vector() * 100; // 100cm }AActor* Ugrabber::lineTrace() {// 檢查碰撞FHitResult hit;GetWorld()->LineTraceSingleByObjectType(hit, getLineStart(), getLineEnd(), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),FCollisionQueryParams(FName(TEXT(""), false, GetOwner()));AActor * act = hit.GetActor();if (nullptr != act){UE_LOG(LogTemp, Warning, TEXT(line hit : % s), *act->GetName());}return act; }總結
以上是生活随笔為你收集整理的UE4学习-添加机关并添加代码控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学生党福音 Redmi小金刚Note 1
- 下一篇: 深得沃尔沃真传 领克08内饰官图发布:典