Why is my USphereComponen always offset?

I use a USphereComponent to detect when enemies or other objects approach the player. It worked perfectly in 4.8, but when I updated to 4.9 it started spawning at an offset from the player. I added a line to the constructor to set its location relative to its component to origin:

USphereComponent* sensoryRadius;
sensoryRadius = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Sensory Sphere"));
sensoryRadius->SetRelativeLocation(FVector(0, 0, 0));
sensoryRadius->InitSphereRadius(250.0f);
sensoryRadius->OnComponentBeginOverlap.AddDynamic(this, &UPerception::OnOverlap);
sensoryRadius->OnComponentEndOverlap.AddDynamic(this, &UPerception::OnEndOverlap);

But this has no effect whatsoever. Every time a character spawns, their sensory sphere is down and to the right of their model, like so:

Is there something else I should be doing in the class constructor to ensure that the sphere is always centered around the player?

Hey HInoue-

When creating the sphere component you want to attach it to whatever component you want it to follow (usually the root component). Setting sensoryRadius->AttachTo(RootComponent); should center the sphere component to your capsule component.

Cheers

Thank you! :slight_smile: As a quick follow-up, this code isn’t actually on an AActor, it’s on an actor component childed to the ACharacter who has the capsule collider. What is the correct syntax to find RootComponent in that case? I tried this, but it actually crashed UE and wouldn’t let it start again until I commented it out and deleted the binaries:

sensoryRadius->AttachTo(GetOwner()->GetRootComponent());

I also tried this, but it failed to compile on account of RootComponent being protected:

sensoryRadius->AttachTo(GetOwner()->RootComponent);

Weirdly enough, adding a pointer safety check solved it, even though there isn’t a context I’m aware of in which GetOwner() can return null… either way it works now, thank you again! :slight_smile: