Download

How to rotate a specific part(Static mesh) of the pawn by the controller?

I’m making a multiplayer game where players can walk up to a turret gun and control it. I can now successfully posses the turret gun, but I want only the “muzzle” part of my turret gun to rotate around with the controller when I look around.

The turret gun is a class derived from APawn which has different parts that make up the gun setup as separate static meshes. Here’s the constructor for an idea on how the class is setup



SetRootComponent(CreateDefaultSubobject<USceneComponent>(TEXT("ConstructRoot")));

constructBase = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Construct Base"));
constructBase->SetupAttachment(GetRootComponent());

rotatingConstructPart = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rotating Construct Part"));
rotatingConstructPart->SetMobility(EComponentMobility::Movable);
rotatingConstructPart->SetupAttachment(GetRootComponent());

muzzle = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Muzzle"));
rotatingConstructPart->SetMobility(EComponentMobility::Movable);
muzzle->SetupAttachment(rotatingConstructPart);
//muzzle->bUseControllerRotationPitch(true);


Ideally, how I want this to work is that the rotatingConstructPart rotates sideways, while the muzzle looks up and down. How can I achieve this? Any help would be greatly appreciated. Thanks!

There are multiple ways to do it, but we do it this way. Fairly straightforward way to switch player control between any number of possessable objects:

  • make an interface like “PlayerControllable” with LookUp(), LookLeft(), TurnLeft(), etc.
  • anything controllable / possessable by player implements the interface
  • primary player controller binds all inputs
  • primary player controller has pointer currently controlled / possesed object
  • primary player controller redirects all inputs to currently controlled / possesed object

So a “turn left” logic flow would look something like:

key pressed >> PlayerController.TurnLeft() >> ControlledObject.TurnLeft()