x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

alt text

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

Product Version: UE 4.9
Tags:
offset.png (279.1 kB)
more ▼

asked Aug 31 '15 at 05:00 PM in C++ Programming

avatar image

HInoue
246 94 65 83

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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

Doug Wilson

more ▼

answered Aug 31 '15 at 08:17 PM

avatar image HInoue Aug 31 '15 at 08:49 PM

Thank you! :) 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);




avatar image HInoue Aug 31 '15 at 09:07 PM

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! :)

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question