CharacterMovement周りを読む
214301428行あるけど絶対に読む
Unreal Engine | UCharacterMovementComponent
CharacterMovementComponentはいろいろ継承しており、親のMovementComponentを見ときたい感じある
MovementComponent
Unreal Engine | UMovementComponent
* MovementComponent is an abstract component class that defines functionality for moving a PrimitiveComponent (our UpdatedComponent) each tick.
* Base functionality includes:
* - Restricting movement to a plane or axis.
* - Utility functions for special handling of collision results (SlideAlongSurface(), ComputeSlideVector(), TwoWallAdjust()).
* - Utility functions for moving when there may be initial penetration (SafeMoveUpdatedComponent(), ResolvePenetration()).
* - Automatically registering the component tick and finding a component to move on the owning Actor.
* Normally the root component of the owning actor is moved, however another component may be selected (see SetUpdatedComponent()).
* During swept (non-teleporting) movement only collision of UpdatedComponent is considered, attached components will teleport to the end location ignoring collision.
とことです。
PawnMovementComponent
Unreal Engine | UPawnMovementComponent
FFindFloorResult
Unreal Engine | FFindFloorResult
CharacterMovementComponentは、ACharacterの中で使われている、以下コンストラクタ
CharacterMovement = CreateDefaultSubobject<UCharacterMovementComponent>(ACharacter::CharacterMovementComponentName); if (CharacterMovement) { CharacterMovement->UpdatedComponent = CapsuleComponent; CrouchedEyeHeight = CharacterMovement->CrouchedHalfHeight * 0.80f; }
//////////////////////////////////////
とりあえず何から手を付けたらいいのやらなので、CharacterがどうやってMoveしているのか周辺からみていく
Controllerからのインプットで移動するときに使うノードは、AddMovementInputで、これはAPawnにベースがあるのでここからスタートする
APawn::AddMovementInput
・UPawnMovementComponentを取得する
・UPawnMovementComponent->AddInputVectorを実行
↓
UPawnMovementComponent::AddInputVector
・自身のオーナー(APawn)のAPawn::Internal_AddMovementInputを実行
↓
APawn::Internal_AddMovementInput
・変数ControlInputVectorに移動ベクトルを追加
AddMovementInput自体はこのような感じで、実際にPawnを動かす処理はしておらず、移動ベクトルを蓄積しているだけで、他に移動を担っている部分があることがわかる、
このControlInputVectorを取得&リセットする、APawn::ConsumeMovementInputVectorという関数や、Internal_GetPendingMovementInputVectorというインライン関数が用意されている
以下のページにも
Unreal Engine | UPawnMovementComponent::AddInputVector
Add movement input along the given world direction vector (usually normalized) scaled by 'ScaleValue'. If ScaleValue < 0, movement will be in the opposite direction.
Base Pawn classes won't automatically apply movement, it's up to the user to do so in a Tick event. Subclasses such as Character and DefaultPawn automatically handle this input and move.
とのことです。
CharacterやDefaultPawnではこの処理がすでに実装されているらしいので、より簡単そうなDefaultPawnの該当部分を探すことにする
DefaultPawnの構成、MovementComponent, SphereComponent, StaticMeshComponentがメンバ変数で、コンストラクタでRootComponentをSphereComponentとし、MovementComponent->UpdatedComponentにSphereComponentを代入している
残りの移動系の関数では、普通にAddMovementInputが出てきているので、Tick系の処理はMovementComponentがやっており、継承元か自身のどこかでやっていることになる
コンストラクタで、MovementComponentをよくみると、UPawnMovementComponentを継承したUFloatingPawnMovementが代入されており、完全にこれ感が漂う
ActorComponentを継承したクラスでは、TickComponentという関数がTickイベントに相当するようで、MovementComponentを見ると、Pawnの死亡を見る程度のことしかやっていない
一方FloatingPawnMovementのほうでは、いろいろやっており、ようやくたどり着いた…
UFloatingPawnMovement::TickComponent
UpdatedComponentがヌル、MobilityがMovableでない、DedicatedServerである、自身と子の前回のレンダリングが0.41sより小さい ならスキップする
↓
SuperのTickComponent(アクターが死んでいるかどうかの確認)
↓
Pawnを操作しているコントローラーがローカルならControlInputVectorの処理をやる、AIなら
↓
ControlInputVectorの処理、UFloatingPawnMovement::ApplyControlInputToVelocity
ControlInputVectorになんか値がある(≒AddInputMovementが実行されている) && 最大速度でないなら
ApplyControlInputToVelocityおわり
↓
Speedは1秒間のアンリアル単位(cm)での移動量で、最大速度を超えている場合は、Velocityを最大速度に丸める
↓
Velocity * DeltaTimeをDeltaとする、Tick内で動く量になる
/////////////////////////
/*
// UpdateComponentはUMovementComponentにあるUSceneComponent*型のメンバ変数で、
The component we move and update.
とのことなので、digりましょう
/
わ、わ、わ、わからない……
// コンポーネントのTickイベントは、AActorのTickのようでなく、コンポーネントの親クラス、UActorComponentの FActorComponentTickFunction PrimaryComponentTick でどうこうやっているよう
// MovementComponentを見ていると、VelocityとかをUSceneComponentのComponentVelocityに代入しているので、そちらを見ます
素敵トピック
Unreal Engine | コンポーネントとコリジョン
PrimaryComponentTick
//CharacterではPawnから何が強化されてるかというと、当たり判定全般の機能や、重力が効いたりしていて、逆に言えばここらへんを弄りたかったら3512513521行のソースコードに目を通すはめになってしまう