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"

Check Overlap for moved Actor

I have to move an Actor (StaticMesh) from C++ using SetActorLocation.

How can I check an overlap with all other Actors on the scene to avoid unnecessary overlap.

Something like this,

 MyActor->SetActorLocation(NewLocation);
 if ( MyActor->"overlap with other actors" )
 {
     // no need to move my actor due to overlap issue.
     MyActor->SetActorLocation(OldLocation);
     // or highlight it
     MyActor->SetRedColor();
 }

The main thing is to find an overlap with all other actors.

Product Version: UE 4.18
Tags:
more ▼

asked Dec 08 '17 at 09:33 AM in C++ Programming

avatar image

SvarogZ
49 10 12 20

avatar image SvarogZ Dec 08 '17 at 12:01 PM

I tried to find an answer... Probably like this? I did'n check yet... Please confirm if it's correct.

 // In constructor
 OnActorBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
 OnActorEndOverlap.AddDynamic(this, &AMyActor::OnOverlapEnd); 
 bGenerateOverlapEvents = true;
 
 // Functions
 void AMyActor::OnOverlapBegin(class AActor* ThisActor, AActor* OtherActor)
 {
     bOverlap = true;
 }
 
 void AMyActor::OnOverlapEnd(class AActor* ThisActor, AActor* OtherActor)
 {
     bOverlap = false;
 }
 
 bool AMyActor::GetOverlapStatus()
 {
     return bOverlap;
 }

And after that

 MyActor->SetActorLocation(NewLocation);
 if (MyActor->GetOverlapStatus())
 {
      // no need to move my actor due to overlap issue.
      MyActor->SetActorLocation(OldLocation);
      // or highlight it
      MyActor->SetRedColor();
 }
(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

There is a function called TeleportTo. Useful for just instantaneously moving to positions. Sweep means that the object will collide with things as it moves to the target location. If you want it to just teleport, make sure sweep is false

void AWarrior::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent *OtherComp, bool  bSelfMoved, FVector HitLocation, FVector HitNormal, FVector  NormalImpulse, const FHitResult &Hit)
{
    Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit); // Important
    AInventoryActor *InventoryItem = Cast
  
   (Other);
    if (InventoryItem)
    {
        TakeItem(InventoryItem);
    }
}
  
This is some random example code I found in one of my projects. So here, when the character hits something, it will check if its an inventory and if it is, will pick it up. For character collision, you could just do an OnComponentBeginOverlap on the character's capsule component. That will work fine but NotifyHit should work also. Try putting this SetActorEnableCollision(true) in character constructor or change collision presets to block. SimulatePhysics also. Just mess around with the collision settings and it will work, I don't remember all the exact settings you have to change.

more ▼

answered Dec 09 '17 at 07:18 PM

avatar image

Ninja_K
75 4 8

avatar image SvarogZ Dec 10 '17 at 05:02 AM
 virtual bool TeleportTo ( const FVector & DestLocation, const FRotator & DestRotation, bool bIsATest,  bool bNoCheck )

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/TeleportTo/

bNoCheck - is true if we should skip checking for encroachment in the world or other actors

I should use bNoCheck=false.

Probably this is exactly that I need. Thank you! I've never used this function before.

I'll test it today.

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

If you want to check for blocking hits, use this and put whatever you want to do inside. Make sure your mesh actually has a collision defined and that collision presets are set to block and GetStaticMeshComponent()->SetNotifyRigidBodyCollision(true) is in your constructor or BeginPlay:

virtual void NotifyHit (class UPrimitiveComponent * MyComp, AActor * Other, class UPrimitiveComponent * OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult & Hit)
{
    UE_LOG(LogTemp, Warning, TEXT("Hit something!");
}

for overlaps, define this function and make sure bGenerateOverlapEvents is set to true in constructor or BeginPlay:
void OnOverlapsBegin(UPrimitiveComponent *Comp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
    UE_LOG(LogTemp, Warning, TEXT("Overlapped something!");
}
Also, you have to bind that overlap delegate (the OnOverlapsBegin function I wrote above). So you would have to do this in BeginPlay or PostInitializeComponents:
virtual void PostInitializeComponents()
{
    GetStaticMeshComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMyStaticMeshActor::OnOverlapsBegin);
}

Finally, you can also use this function for overlaps which is much simpler if you're not comfortable with using delegates:
virtual void NotifyActorBeginOverlap(AActor *OtherActor)
{
    UE_LOG(LogTemp, Warning, TEXT("Overlapped something!");
}

more ▼

answered Dec 08 '17 at 06:04 PM

avatar image

Ninja_K
75 4 8

avatar image SvarogZ Dec 09 '17 at 04:43 AM

Thank you very much. There is very detailed and useful answer. I'll check this solution.

My StaticMesh does not have collision. Before mounting it looks like phantom and moving in the client side only. In fact I just need to move my StaticMesh to totally empty places (no any actors in that volume). And (ideally) before changing location, I want to know is it empty or not.

This is an implementation of constraction from blocks.

avatar image SvarogZ Dec 09 '17 at 06:14 AM

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/NotifyHit/

Event when this actor bumps into a blocking object, or blocks another actor that bumps into it. This could happen due to things like Character movement, using Set Location with 'sweep' enabled, or physics simulation.

Set Location with 'sweep'?

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/SetActorLocation/

Whether we sweep to the destination location, triggering overlaps along the way and stopping short of the target if blocked by something. Only the root component is swept and checked for blocking collision, child components move without sweeping. If collision is off, this has no effect.

My collision is off. If it's on, moved actor bumps all actors around.

Is it possible to use this only?

 SetActorLocation(NewLocation, true)

In that case new location will not be assigned? It means I can switch on the collision in advance without any risk to bump other actors. I'll check today. If it works, it's the best solution for me.

avatar image SvarogZ Dec 09 '17 at 05:42 PM

SetActorLocation(NewLocation, true) works. I even can use it...

However, my actor is stuck from any obstacle. It's not good.

I don't understand how NotifyHit function works. It works in case of bump with main character only. And doesn't work with another client character. I put this function to the class of moved block.

Could you give an example?

Thanks in advance.

(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