没报错,但根本没调用抓取那个if
Grabber.cpp :
// Fill out your copyright notice in the Description page of Project Settings.
#include "Grabber.h"
#include "Engine/World.h"
#include "DrawDebugHelpers.h"
#include "Components/PrimitiveComponent.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();
PhysicsHandle=GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
GrabInput=GetOwner()->FindComponentByClass<UInputComponent>();//通过<类型>的class来查找组件
if (GrabInput){
UE_LOG(LogTemp, Warning, TEXT("Input Component is founded!"));//日志输出
//IE_Pressed表示按下。在""内的Pressed状态下会触发Grab()函数事件,函数事件要加类名,还要取引用
GrabInput->BindAction("Grab",IE_Pressed,this, &UGrabber::Grab);
GrabInput->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}else{
UE_LOG(LogTemp, Error, TEXT("Input Component is not founded!"));//日志输出
}
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//射线检测,需要物体勾选Simulate Physics这个属性
LineTrace();
}
FVector UGrabber::GetLineStart() {//获得射线开始点
FVector ViewStart;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(ViewStart, ViewRotator);//获得视野正中心并赋值,第一个参数为坐标,第二个为旋转
return ViewStart;
}
FVector UGrabber::GetLineEnd() {//获得射线结束点
FVector ViewStart;
FRotator ViewRotator;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(ViewStart, ViewRotator);
return ViewStart+ViewRotator.Vector()*100;
}
FHitResult UGrabber::LineTrace() {//射线检测
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType(
Hit,//返回碰到物体的类型
GetLineStart(),
GetLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), //检测碰到了哪些物体,ECC_PhysicsBody表示有物理和物理效果的物体
FCollisionQueryParams(FName(TEXT("")), false, GetOwner())//GETOwner就是把所在的Owner赋值给FCollisionQueryParams
);
//AActor* HitActor = Hit.GetActor();//获得碰到了哪些Actor,返回AActor*类型
return Hit;
}
void UGrabber::Grab() {
UE_LOG(LogTemp, Warning, TEXT("Grab is founded!"));//日志输出
FHitResult Hit=LineTrace();
if (Hit.GetActor() && PhysicsHandle){
UE_LOG(LogTemp, Warning, TEXT("Grab!"));//日志输出
PhysicsHandle->GrabComponentAtLocation(Hit.GetComponent(), NAME_None, Hit.GetComponent()->GetOwner()->GetActorLocation());
//(Hit.GetComponent,NAME_None,Hit.GetComponent().GetOwner()->GetActorLocation(),true);
//参数一为需要抓取的组件,参数二为绑定骨骼的名称(Name_None表示为空),参数三为要抓取物品的位置
}
}
void UGrabber::Release() {
UE_LOG(LogTemp, Warning, TEXT("Release is founded!"));//日志输出
}
Grabber.h :
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/InputComponent.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Grabber.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class SIKIEDU_MSTT_API UGrabber : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabber();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
FVector GetLineStart();//获得射线开始点
FVector GetLineEnd();//获得射线结束点
FHitResult LineTrace();//射线检测
UInputComponent* GrabInput=nullptr;
UPhysicsHandleComponent* PhysicsHandle=nullptr;
void Grab();//按下抓取
void Release();//抬起释放
};
就Grab()里面的if根本没进去:


