UE4 C++
使用addforce函数时,可以生成解决方案,但是在ue4按下模拟运行时,先是卡一下啊,然后闪退,再报错。
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Mycube.generated.h"
UCLASS()
class TEST_API AMycube : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
///
/// 声明模型变量
///
UPROPERTY(EditAnywhere,Category="ActorMeshCompoment")
UStaticMeshComponent* StaticMesh;
///
/// 位置信息
///
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "MyValues")//场景中可见
FVector InitialLocation;//初始位置
///
/// 使用xxx功能吗?
///
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
bool use_InitialLocation;//使用初始位置吗
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
bool Use_World;//使用世界坐标吗?
///
/// 移动和旋转方向
///
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
FVector Initial_Direction;//移动方向
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
FRotator Initial_Rotation;//旋转方向
///
/// 力和扭矩
///
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
FVector InitialForce;//力
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyValues")
FVector InitialTorque;//扭矩
protected:
AMycube();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "Mycube.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AMycube::AMycube()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
AActor::PrimaryActorTick.bCanEverTick = true;
StaticMesh = CreateDefaultSubobject(TEXT("MyMesh"));//创建默认子物体<类型>(名字)
//设置初始位置
InitialLocation = FVector(-100,350,420);
//设置方向
Initial_Direction = FVector(1, 0, 0);
//设置旋转
Initial_Rotation = FRotator(90, 0, 0);
//设置力和扭矩
InitialForce = FVector(0);
InitialTorque = FVector(0);
}
// Called when the game starts or when spawned
void AMycube::BeginPlay()
{
Super::BeginPlay();
if (use_InitialLocation) {
SetActorLocation(InitialLocation);
}
StaticMesh->AddForce(InitialForce, TEXT("NAME_None"), false);
//StaticMesh->AddTorque(InitialTorque);
}
// Called every frame
void AMycube::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FHitResult HitResult;//声明变量
/*if (Use_World) {//根据世界坐标移动
AddActorWorldOffset(Initial_Direction, true, &HitResult);//添加对象移动/移动式是否开启扫描/扫描(碰撞)结果
AddActorWorldRotation(Initial_Rotation);
}
else {
AddActorLocalOffset(Initial_Direction, true, &HitResult);//添加对象移动/移动式是否开启扫描/扫描(碰撞)结果
AddActorLocalRotation(Initial_Rotation);
}*/
/*if (HitResult.bBlockingHit) {
UE_LOG(LogTemp, Warning, TEXT("@@f Hit@:x=%f,y=%f,z=%f"),HitResult.Location.X, HitResult.Location.Y, HitResult.Location.Z);
}*/
if (HitResult.bBlockingHit) {
UE_LOG(LogTemp, Warning, TEXT("@@s Hit@:x=%s,y=%s,z=%s"),*FString::SanitizeFloat(HitResult.Location.X), *FString::SanitizeFloat(HitResult.Location.Y),* FString::SanitizeFloat(HitResult.Location.Z));
}
}
回答不易,求求您采纳点赞哦
您遇到的“addforce”函数导致虚幻引擎 4 闪退错误的问题可能是由几个不同的潜在问题引起的。以下是一些可能的原因和解决方案:
施加的力太大或施加得太快。这可能会导致物理模拟变得不稳定并导致闪回。尝试减少施加的力或增加力施加之间的时间。
施加力的物体太重或质量太大。这可能会导致物理模拟变得不稳定并导致闪回。尝试减少物体的质量或减少施加的力。
施加力的对象未正确配置以进行物理模拟。确保对象在对象设置中启用了“模拟物理”选项,并且正确设置了碰撞网格。
代码本身可能存在问题,例如无限循环或空指针。检查您的代码并确保没有无限循环或其他可能导致模拟冻结的问题。
您可能需要检查您的系统要求,以确保它们满足运行模拟的最低要求。
需要注意的是,以上建议只是导致问题的几种可能原因,需要进行彻底的调试。查看 Unreal Engine 4 文档或论坛以获取有关解决物理相关问题的更多信息也可能会有所帮助。