Attach Spring Arm to Character

If you attach the spring to your character, to be able to control it location and rotation via SetWorldLocationAndRotation , use this SetAbsolute( bool bNewAbsoluteLocation,bool bNewAbsoluteRotation, bool bNewAbsoluteScale)

Recently I’m trying to split the connection between camera, spring arm, and character in order to attain more

flexibility

in my original design, the inter-class connection should looked like this:

(character class)<-(Spring arm class)<-camera

the arrow between indicates that these different actors(or character) are connected via AttachTo() function
(note that these are not components in same class, instead, they are in different class)

the code works fine when I’m attaching camera to Spring Arm class

However, there are some trouble while i’m trying to Attaching Spring Arm instance into character.
It ends that when I’m trying to attach the spring arm, it will result in camera being in wired location or rotation

the code is simplified as follows:

//attaching spring arm to character

m_CameraBoom->SetActorLocationAndRotation(

m_ControllingCharacter->GetRootComponent( )->GetComponentLocation( ),
m_ControllingCharacter->GetRootComponent( )->GetComponentRotation( ));


//if I delete this line of code, the camera will back to normal.

m_CameraBoom->AttachRootComponentTo( m_ControllingCharacter->GetRootComponent( ) );


//attaching camera to spring arm, set camera’s location & rotation to the tail of spring arm

m_Camera->SetWorldLocationAndRotation( m_CameraBoom->GetCameraAttachComponent( )->GetSocketLocation( m_CameraBoom->GetSocketName( ) ), m_CameraBoom->GetActorRotation( ) );


//attach camera to spring arm, this line of code works great as long as the spring arm is not
attached to character

m_Camera->AttachTo( m_CameraBoom->GetCameraAttachComponent( ), m_CameraBoom->GetSocketName( ) );


I’ve stuck in this issue for a long time, hopefully anyone can give me a guideline.

You have to call SetWorldLocationAndRotation every frame

I’ve call this API before SetWorldLocationAndRotation by setting all of the three fields to true, it indeed fix the spring arm location issue. But when the character is moving, the spring arm is not following the character. Did I do anything wrong?

Yes, it works fine if I call it in Tick, however, my intention is attaching it to the character instead of refresh its location and rotation every frame, any solution for it? Thank you so much.

If i understand well in your CameraBoom class your created a new camera component and attached it to the CameraBoom.
And inside your Character class you created an instance of your CameraBoom in the constructor?

No, actually it looked like this:
MyPlayerController:
1.It has a pointer of CameraBoom, a pointer of character, and a camera component
2.in this class, it will spawn the CameraBoom, attach it to character and attach camera component to it

This should work

m_CameraBoom->AttachTo(m_ControllingCharacter->GetRootComponent()); 

m_CameraBoom->SetRelativeLocation(FVector::ZeroVector);

m_Camera->AttachTo(m_CameraBoom, m_CameraBoom->SocketName, EAttachLocation::SnapToTarget);

Actually this is the approach I’ve tried above.
It won’t follow the character and spring arm will be in a weird location

( (0, 0, 0) I guess )

m_CameraBoom->GetRootComponent( )->SetAbsolute( true, true, true );

m_CameraBoom->AttachRootComponentTo( m_ControllingCharacter->GetRootComponent( ) );

m_CameraBoom->SetActorRelativeLocation( FVector::ZeroVector );

m_Camera->AttachTo( m_CameraBoom->GetCameraAttachComponent( ), m_CameraBoom->GetSocketName( ) );

delete
m_CameraBoom->GetRootComponent( )->SetAbsolute( true, true, true );
because this means boom location,rotation and scale will not depend on it rootcomponent

I’ve tried before, it will leads to camera being more weird location.

for you camera use this

m_Camera->AttachTo(m_CameraBoom, m_CameraBoom->SocketName, EAttachLocation::SnapToTarget);

or

m_Camera->AttachTo(m_CameraBoom, m_CameraBoom->SocketName);

m_Camera->SetRelativeLocationAndRotation(FVector::ZeroVector, FRotator::ZeroRotator);

Still doesn’t work, camera will stuck in character’s body.

Although it can move with character.


m_CameraBoom->AttachRootComponentTo( m_ControllingCharacter->GetRootComponent( ) );

m_CameraBoom->SetActorRelativeLocation( FVector::ZeroVector );

m_Camera->AttachTo( m_CameraBoom->GetCameraAttachComponent( ), m_CameraBoom->GetSocketName( ) );

m_Camera->SetRelativeLocationAndRotation( FVector::ZeroVector, FRotator::ZeroRotator );

It turns out that there’s something wrong with the camera’s relative location.
I’ll figure that out later

I’ve given up this approach since there’s not viable way to prevent camera be in the character’s rootcomponent location

I’ve tried to use component structure as follow:
class CameraSystem:AActor

{

root: spring arm1;

spring arm2 attached to spring arm 1;

camera attached to spring arm

}

and I’m pretty sure the camera is active, the problem lies in when I’m trying to attach its rootcomponent to
character, the camera will be in the same world location of character, which makes
spring arm 1 and 2 completely useless.

And I tried to inherit from class Character by making its root mesh invisible, the result works perfectly fine as I expected to see.

I guess it’s the attach operation works differently in the two classes, which results in weird camera position.