关键类
本篇博文用到的关键类有:
UInputComponent 用来绑定鼠标的按下和释放事件 BindAction
UPhysicsHandleComponent 设置被抓取物体的抓取,移动,释放
GrabComponentAtLocationWithRotation
ReleaseComponent
SetTargetLocation
UPrimitiveComponent 抓取组件抓取的对象
还可以获取质量 GetMass
鼠标按下、释放事件
首先,添加鼠标事件,在项目设置中,引擎->输入->操作映射,按加号,添加一个映射对象,然后添加一个鼠标左键事件,一个空格键事件。

在代码中创建一个 UInputComponent对象。
在grabber.h文件中,定义个人建议使用前置声明,把头文件放在cpp里面去包含。
并且添加两个函数,一个表示按下,一个表示释放。
#include <Components/InputComponent.h>
void Grab();
void Release();
UInputComponent* inputComponent = nullptr;
在cpp文件中添加:
// Called when the game starts
void Ugrabber::BeginPlay()
{
Super::BeginPlay();
inputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (nullptr != inputComponent)
{
UE_LOG(LogTemp, Warning, TEXT("find inputComponent"));
inputComponent->BindAction("grab", IE_Pressed, this, &Ugrabber::Grab);
inputComponent->BindAction("grab", IE_Released, this, &Ugrabber::Release)

本文详细介绍了如何在UE4中实现物体的抓取、移动与重量计算功能。通过绑定鼠标事件,利用UPhysicsHandleComponent组件,实现了物体的抓取与释放。同时,通过UPrimitiveComponent组件获取物体质量,结合触发器组件,实现了基于物体总重量的机关门开启机制。
&spm=1001.2101.3001.5002&articleId=107093499&d=1&t=3&u=27967338cf3c49de83f2317f24a226e3)
1068

被折叠的 条评论
为什么被折叠?



