UPDATE!
I solved it with what I see as a work-around (or maybe it’s intended?) below. See the answer.
My first time posting here, so if I break etiquette in my question or if more information is needed please let me know in a comment so that I can improve my questions in the future.
- The goal of this is to have the sword
attach to the player, and begin
moving along with it, as if you have
picked it up.
I currently have a StaticMesh rendered in the world through the editor, through a blueprint. (Just a basic sword model I found online.) I am casting a ray out from my character and when it hits this sword, I am attempting to have the sword snap and attach to the player. However, it doesn’t seem to be working.
Everything works fine, the raycast, the detection of the raycast smacking the sword, and even if I use a Destroy() on the sword with the raycast, it does great! Just not this attachment situation.
Also something weird: If I attempt to attach the PLAYER to the SWORD… it works. Any ideas? I will share my full code below that I think is relevant (the ‘OnFire’ function from my C++ that happens when you left click.)
void ATheCharacter::OnFire()
{
FHitResult hitresult(ForceInit);
//Raycast, feeding the results to the memory location of 'hitresult'
Raycast(&hitresult); //player
obj = hitresult.GetActor();
//Make sure we are hitting an actor before going any further to avoid null
if (hitresult.GetActor())
{
//Display on screen to make sure the right things are being used
GEngine->AddOnScreenDebugMessage(-10, 1.f, FColor::Yellow, obj->GetName());
//Keep from doing work on unintended actors
if (hitresult.GetActor()->ActorHasTag("NotWorld"))
{
//obj->Destroy(); //WORKING!
obj->AttachRootComponentTo(this->GetRootComponent()); //NOT WORKING!
}
}
else
{
GEngine->AddOnScreenDebugMessage(-5, 1.f, FColor::Red, "No Actor Detected");
}
}
Below, I have also added my Raycast function, in case that is relevant to what’s making this not work.
void ATheCharacter::Raycast(FHitResult* hit)
{
//If we aren't possessing something, then stop, because we can't get a viewpoint or raycast from null
if (Controller == NULL) return;
//Camera position, Camera rotation, Raycast start position, Raycast end position
FVector CameraLoc;
FRotator CameraRot;
FVector StartPos;
FVector EndPos;
//EndPos and StartPos for the trace, just ahead of the actor/camera
EndPos.Set(500.0f, 500.0f, 500.0f);
StartPos.Set(200.0f, 200.0f, 200.0f);
//Load the controlled actor's forward direction and rotation into CameraLoc and CameraRot
GetActorEyesViewPoint(CameraLoc, CameraRot);
//Setting up the start and end of the raycast
FVector Start = CameraLoc + (CameraRot.Vector() * StartPos);
FVector End = CameraLoc + (CameraRot.Vector() * EndPos);
//Do the raycast
GetWorld()->LineTraceSingle(*hit, Start, End, ECC_MAX, 0);
DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), true, 10, 0, 3);
}