11715人加入学习
(26人评价)
【旧版】Unreal基本知识案例 - 密室逃脱

旧版课程,制作完成于2017-01-11

价格 免费

编辑---项目设置---引擎--输入

Bindings

Action Mappings(某个键的按下)

Axis Mappings(某个轴的按下)

 

 

2个大步骤:

(1)在编辑---项目设置---引擎--输入-Bindings-Action Mappings(某个键的按下),添加事件再添加需要触发的按键

(2)写代码

 

 

 

添加上以后,运行场景系统在DefaultPawn_Blueprint的细节面板上有PawnInputComponent(继承):

 

 

 

#include "Components/InputComponent.h"

#include "Grabber.generated.h" // 引入的头文件需要再这个头文件的前面

UInputComponent* InputComponent = nullptr;

使用 UInputComponent* 需要引入 #include "Components/InputComponent.h"

 

 

 

// Grabber.h

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/InputComponent.h"
#include "Grabber.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UNREALENGINEPROJECT_API UGrabber : public UActorComponent
{
	GENERATED_BODY()
public:	
	UGrabber();
protected:
	virtual void BeginPlay() override;
public:	
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

private:
	FVector GetLineStart();
	FVector GetLineEnd();
	AActor* LineTrace();
	UInputComponent* InputComponent = nullptr;
	void Grab();
	void Release();
};

 

 

// Grabber.cpp

#include "Grabber.h"
#include "Engine/World.h"
#include "DrawDebugHelpers.h"


UGrabber::UGrabber()
{
	PrimaryComponentTick.bCanEverTick = true;
}


void UGrabber::BeginPlay()
{
	Super::BeginPlay();
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();

	if (InputComponent)// 使用指针需要判断不为空
	{
		UE_LOG(LogTemp, Warning, TEXT("Input Component is founded!"));
		InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
		InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("Input Component is not founded!"));
	}
	
}


void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

FVector UGrabber::GetLineStart()
{
	FVector Start;
	FRotator ViewRotator;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
	return Start;
}

FVector UGrabber::GetLineEnd()
{
	FVector Start;
	FRotator ViewRotator;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(Start, ViewRotator);
	return (Start + ViewRotator.Vector() * 100); // ViewRotator.Vector() 视野的正中心 前方 100cm
}

AActor * UGrabber::LineTrace()
{
	//DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), false, 0, 0, 10);

	FHitResult Hit;
	GetWorld()->LineTraceSingleByObjectType(
		Hit,
		GetLineStart(),
		GetLineEnd(),
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
		FCollisionQueryParams(FName(TEXT("")), false, GetOwner())
	);
	AActor* Actor = Hit.GetActor();
	if (Actor != nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("Line Hit:%s"), *Actor->GetName());
	}
	return Actor;
}

void UGrabber::Grab()
{
	UE_LOG(LogTemp, Warning, TEXT("Grab action is pressed!"));
}

void UGrabber::Release()
{
	UE_LOG(LogTemp, Error, TEXT("Grab action is released!"));
}

 

[展开全文]

授课教师

SiKi学院老师

课程特色

图文(2)
视频(37)