Using TeleportTo() With Objects/Actors

I have the following code:

if (bHoldingItem)
{

if (GetWorld()->LineTraceSingleByChannel(*HitResult, StartTrace, EndTrace, ECC_Visibility, *TraceParams))
{

DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), true);

TeleportTo(FVector(HitResult->Actor->GetActorLocation()), GetWorld()->GetFirstPlayerController()->GetControlRotation(), false, true);

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(“You hit: %s”), *HitResult->Actor->GetName()));

}

Although not everything, I have pickup class which allows objects with the class to be picked up by the character. During the pick up, the object in default would be situated right smack on the middle of the crosshair. The player is then able to carry the object around, continuing its relative location.

However, I am trying to do something along the lines of this video (Superliminal Perspective Scaling (Unreal Engine 4) - YouTube) which employs forced pespective. Therefore in the code above, right when the character picks up an object (bHoldingItem = true), it will trace rays, and it will return whichever actor is in current contact with the trace line. At this moment, I want the picked-up object to be teleported right to the location of where the trace line hits the actor. Of course I am not tracing lines around the object’s vertices just quite, but I am just testing stuff right now before do that process.

And that is when I tried to use the TeleportToFunction; however, instead of the picked-up object being teleported, it is teleporting the player. Anyway to fix this? I am new to Unreal C++, so excuse me if this may seem rudimentally.

Got a school project to do folks!

The code you posted is in your Character class, correct?

If you already have the Actor which is being held (I’ll call it HeldItem) skip to step 3, otherwise you’ll first need to remember it.

  1. add this to your Character class header file

UPROPERTY()
AActor * HeldItem;

  1. wherever you set bHoldingItem, also remember/reset the Actor

// when you set bHoldingItem = true; remember the Actor, probably something like HitResult.Actor if you're using a trace to determine the held item
HeldItem = TODO;


// don't forget to clear the HeldItem when you set bHoldingItem = false;
HeldItem = nullptr;

  1. instead of calling TeleportTo on this (the character), call it on the HeldItem

if (HeldItem)
{
   HeldItem->TeleportTo(FVector(HitResult->Actor->GetActorLocation()), GetWorld()->GetFirstPlayerController()->GetControlRotation(), false, true);
}

  1. Profit

Alright, it seems to have worked; had a variable called CurrentItem which is basically the same as your HeldItem.

But another problem has risen. Once a line trace has hit an actor, the object will then teleport to that location, but unless the line trace hits another actor, the object will continue to stay in that location until so. I want the object to constantly move to the location of the hit. I’ll probably have to change

CurrentItem->TeleportTo(FVector(HitResult->Actor->GetActorLocation()), GetWorld()->GetFirstPlayerController()->GetControlRotation(), false, true);

just a bit, but my lack of UE4 vocabulary has gotten me stuck.
Thanks again!

Edit: nvm, got it to work! Instead of (HitResult->Actor->GetACtorLocation()), I did (HitResult->ImpactPoint), and it seems to work!