设置椅子和桌子的质量
细节面板上的Physics---MassInKg打勾并设置质量
报错是因为没有引入 UPrimitiveComponent 所需要的头文件
#include "Components/PrimitiveComponent.h"
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
UE_LOG(LogTemp, Warning, TEXT("Total Mass is %f!"), TotalMass);// 加 * 号是取指针的数据,%f 对于小数类型,直接写小数就行,和C语言printf()一样
为什么显示还是质量0,因为没有勾选上 Generate Overlap Events
检测方式要一致:代码:TriggerVolume->GetOverlappingActors(OUT Actors) 与 Generater Overlap Events 对应
主角没有重力,勾选上 Simulate Physics
Physics---Simulate Physics,勾选上就会有重力的效果
#include "Components/PrimitiveComponent.h"
void UOpenDoor1::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//if (TriggerVolume != nullptr && TriggerVolume->IsOverlappingActor(DefaultPawn))
if (TriggerVolume && GetTotalMassInTrigger() > 25)
{
OpenDoor();
LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}
if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorOpenDuration)
{
CloseDoor();
}
}
float UOpenDoor1::GetTotalMassInTrigger()
{
TArray<AActor*> Actors;
if (TriggerVolume == nullptr)
return 0.0f;
TriggerVolume->GetOverlappingActors(OUT Actors);// OUT 在语法上没有任何影响,方便阅读代码的
float TotalMass = 0;
for (AActor* Actor : Actors)// for(const auto& Actor : Actors)
{
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
UE_LOG(LogTemp, Warning, TEXT("Total Mass is %f!"), TotalMass);// 加 * 号是取指针的数据,%f 对于小数类型,直接写小数就行,和C语言printf()一样
return TotalMass;
}