1. 程式人生 > >【UE4】 第05講 發射物方向調整

【UE4】 第05講 發射物方向調整

(版權宣告,禁止轉載)

       角色發動技能的時候,需要預定義預設發射方向,如果發現方向不是自己預期的,就需要去做調整,需要用到FTransform,FRotator,FQuat,實際上就是矩陣和四元數的計算問題了。

                              
 

                       預設獲得右手骨骼座標系的X軸作為發射方向,紅色的那個軸,但是這個方向不合理

                              

 

        對X軸進行方向調整,變換流程是 左手骨骼區域性座標系->世界座標系->繞Z軸旋轉-70->繞Y軸旋轉-10 即得到合理的方向

 

// 獲取指定bone的transform
const int32 RHandIndex = GetMesh()->GetBoneIndex(TEXT("Bip01-R-Hand"));
const FTransform RHandTransform = GetMesh()->GetBoneTransform(RHandIndex);

// 設定發射物的初始軌道。
FVector LaunchDirection ;
FRotator RotOffsetZ(0.f,-70.f,0.f);
FRotator RotOffsetY(-10.f,0.f,0.f);
FQuat Rotator = RHandTransform.GetRotation();
Rotator = Rotator*RotOffsetZ.Quaternion()*RotOffsetY.Quaternion();

LaunchDirection = Rotator.GetAxisX();