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"

Alternatives to LineTrace?

Hi,

I am trying to implement an item pickup system when using a fixed camera. I have it working fine with the code below, but I am doing a linetrace to detect if an item is in front of the player pawn and that means getting the player to look down to pickup items on the floor and up if its above the ground which is impractical with the camera. What I would like to do is do a shapetrace (like a cone) or multiple linetraces to check multiple angles for a collectible item at multiple y values. I was hoping someone might have some advice about a good way too achieve this?

 /*
 Performs ray-trace to find closest looked-at UsableActor.
 */
 ASUsableActor* AOutbreakCharacter::GetUsableInView()
 {
     FVector CamLoc;
     FRotator CamRot;
 
     if (Controller == nullptr)
         return nullptr;
 
     //Controller->GetPlayerViewPoint(CamLoc, CamRot);
     Controller->GetActorEyesViewPoint(CamLoc, CamRot);
     const FVector TraceStart = CamLoc;
     const FVector Direction = CamRot.Vector();
     const FVector TraceEnd = TraceStart + (Direction * MaxUseDistance);
 
     FCollisionQueryParams TraceParams(TEXT("TraceUsableActor"), true, this);
     TraceParams.bTraceAsyncScene = true;
     TraceParams.bReturnPhysicalMaterial = false;
 
     /* Not tracing complex uses the rough collision instead making tiny objects easier to select. */
     TraceParams.bTraceComplex = false;
 
     FHitResult Hit(ForceInit);
 
     GetWorld()->LineTraceSingleByChannel(Hit, TraceStart, TraceEnd, ECC_Visibility, TraceParams);
     GetWorld()->
 
     //GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, Orientation, ECC_Visibility, FCollisionShape::MakeBox(BoxHalfSize), TraceParams);
     //const FVector& BoxHalfExtent
     //GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, CamRot, ECC_Visibility, FCollisionShape::MakeBox(BoxHalfSize), TraceParams);
 
     DrawDebugLine(GetWorld(), TraceStart, TraceEnd, FColor::Red, false, 1.0f);
 
     //DrawDebugLine(GetWorld(), TraceStart, TraceEnd, FColor::Red, false, 1.0f);
 
     return Cast<ASUsableActor>(Hit.GetActor());
 }
Product Version: UE 4.10
Tags:
more ▼

asked Jan 27 '16 at 07:23 PM in C++ Programming

avatar image

johnparkes
22 12 10 16

avatar image rantrod Jan 28 '16 at 01:07 AM

I normally do:

1) check if player is close enough to pickup

2) check if player's camera's forward vector is aimed more or less at pickup. (The angle between the camera's forward vector and a vector from the camera to the pickup)

3) do 1 line trace to make sure the object is not on the other side of a wall or other obstacle

avatar image virtual_nomak Jan 28 '16 at 01:13 AM

Can't you just add a collision sphere and check for an overlap with your player mesh?. If not then maybe a shape trace would do, here is the relevant documentation: ShapeTrace

avatar image johnparkes Jan 28 '16 at 10:39 AM

Thanks, I am trying to do a shape trace with a box, but I am having a bit of trouble creating the actual shape. I was hoping to create a box roughly the same size as the player pawn so it would detect items on the floor and eye level etc. so I thought I would just need to give it a vector with a big Y value. Also, I thought a capsule might be better, but it seems to stretch behind thee player actor when I only really want it in front. Is there a way to render the shape like you can with debug line?

avatar image CalCof Jan 28 '16 at 03:00 PM

Try this: DrawDebugBox(GetWorld(), TraceStart, UseBoxHalfSize, FColor::Red, false, 1.0f, 0);

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

As many others have suggested perform a Multi-Sphere Trace which will get all items within a radius of the sphere. This is pretty fast performance wise. If you have multiple items that could theoretically be within the bounds and you want to only pickup the closest one you are facing you and first determine if your character is looking in the same direction as the item. There are a few methods to do this using line traces, sweeps, or even the Dot Product to determine which item the character is "most" looking at.

more ▼

answered Jan 28 '16 at 01:29 AM

avatar image

ArcainOne
499 32 25 57

avatar image johnparkes Jan 28 '16 at 01:15 PM

Thanks for the advice, I am using the single shape trace sweep method, but I am having some trouble getting the size of the shape right and its a tedious process doing it trial and error. Do you know if there is a way to render the trace shape like you can with a debug line?

This is how I am currently calling it:

 FVector BoxHalfSize(30, 30, 120);
 
     //GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, TraceRot, ECC_Visibility, FCollisionShape::MakeCapsule(100.0f, 80.0f), TraceParams);
     GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, TraceRot, ECC_Visibility, FCollisionShape::MakeBox(BoxHalfSize), TraceParams);
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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