0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unreal Engine:UPhysicsHandleComponentでグラブしたはずのアクターをグラブできない時の解決方法 #CPP

Last updated at Posted at 2022-10-16

Rayを当てたりして取得したアクターUPhysicsHandleComponentGrabComponentAtLocationWithRotationを使って物体を取得し保持することはよくある方法だと思いますが正常に取得できないことがあります。それの解決方法の備忘録。

grabActorSample.cpp
	UPhysicsHandleComponent* handle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if (handle == nullptr) return;

	UPrimitiveComponent* hitComponent = hitResult.GetComponent();
	handle->GrabComponentAtLocationWithRotation(
        hitComponent, 
        NAME_None, 
        result.ImpactPoint, 
        GetComponentRotation()
    );

原因

取得したオブジェクトのPhysicsが寝てしまっている。

解決方法

UPrimitiveComponentWakeAllRigidBodiesを使って、取得したオブジェクトを起こしてやります。

grabActorAwakedSample.cpp
	UPhysicsHandleComponent* handle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if (handle == nullptr) return;

	UPrimitiveComponent* hitComponent = hitResult.GetComponent();
	hitComponent->WakeAllRigidBodies();  //これを追加
	handle->GrabComponentAtLocationWithRotation(
        hitComponent, 
        NAME_None, 
        result.ImpactPoint, 
        GetComponentRotation()
    );

簡単だけど知らないとはまる。

因みにリリースする時も同様

releaseActorAwakedSample.cpp
	UPhysicsHandleComponent* handle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if (handle == nullptr) return;

	// Release grabbed object
	if (handle->GetGrabbedComponent())
		handle->GetGrabbedComponent()->WakeAllRigidBodies();
		handle->ReleaseComponent();

おしまい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?