UE4学习-鼠标事件(按下、释放、物体抓取、计算重量、触发开门)

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

关键类

本篇博文用到的关键类有:

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)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值