Download

Pickup item with raycasting doesn't work..

Hello Unreal developers,

I’m trying to create raycast pickup on items. So basicly here is my idea:

  1. I create a class Item which holds few variables like Name, Price, Type, etc. Plus I have created SphereComponent for collision and pointer for the StaticMesh
  2. Inside the editor I create a blueprint which extends from Item class (I set variables there and set the mesh)
  3. Spawn it inside the level and hit play and try to pickup the item but it doesn’t work… here’s the code for the pickup function:


void ATTSCharacter::UseE()
{
	FHitResult Hit(ForceInit);
	FVector StartTrace, EndTrace;

	FVector CameraLoc;
	FRotator CameraRot;
	GetActorEyesViewPoint(CameraLoc, CameraRot);

	StartTrace = CameraLoc;
	EndTrace = CameraLoc + (CameraRot.Vector() * 500.f) + FVector(0, 0, 25);
	
	ATTSPlayerController* PC = Cast<ATTSPlayerController>(Controller);
	FCollisionQueryParams LineParams(FName("Collision params"), true, PC->GetPawn());

	UWorld* World = GetWorld();

	if (World)
	{
		World->LineTraceSingle(Hit, StartTrace, EndTrace, COLLISION_TRACE, LineParams);
		DrawDebugLine(World, StartTrace, EndTrace, FColor::Cyan, false, 5.0f);

		if (Hit.Actor != NULL)
		{
			GEngine->AddOnScreenDebugMessage(9, 1.0f, FColor::Green, TEXT("Hit something"));

			ATTSItem* HitMesh = Cast<ATTSItem>(Hit.GetActor());

			if (HitMesh)
			{
                                //Just to see if it works..
				HitMesh->bHidden = true;
			}
		}
		else
		{
			GEngine->AddOnScreenDebugMessage(9, 1.0f, FColor::Red, TEXT("Didn't hit anything"));
		}
	}
}


The trace hits stuff of course but casting doesn’t work properly… I don’t know what’s the problem guys, can’t figure it the whole day spent in searching the net…

Here a picture:
44f3044401.jpg

So any ideas?

For the sphere, have you done the C++ equivalent of setting the collision setting so that it blocks traces?

preset.png

Edit:



USphereComponent * MySphere;
MySphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);


Nope I didn’t know I needed to do that… let me try to add it.

Just added the code but it still doesn’t work… It just says Hit something… Or there is something else I should do?

EDIT: Forgot to setup collision parameters in the editor… now it works perfectly thanks! :slight_smile:

Glad it works, you’re welcome!

A new problem occurs…

Now the casting works perfectly fine but I do this:



//In header file:
UPROPERTY(EditAnywhere, Category = "Inventory")
		TArray<ATTSItem*> Inventory;

                        //Inside a function
                        ATTSItem* HitMesh = Cast<ATTSItem>(Hit.GetActor());

			if (HitMesh)
			{
				Inventory.Add(HitMesh);
				HitMesh->Destroy();
			}


It doesn’t seem like it’s getting added to the inventory…

I hit play, pick the item, it dissapears from the world but doesn’t get added to the inventory. I tried commenting HitMesh->Destroy(); but it still wasn’t getting added… I tried creating a new copy and add it to the inventory but still didn’t work. Any ideas?

How and when do you check whether it is added? Since it does disappear, apparently Destroy() is called. Try checking immediately after Inventory.Add whether its added, like did the array size change?

I do make a check like this:
Spawn in the game, press E, function executes, I get message Hit something, then I hit Eject button, click on my character, then Edit Blueprint, and when I scroll down to inventory section, it says 0 elements like this:

http://puu.sh/dT3pj/677a746d30.jpg

http://puu.sh/dT3sr/44d0170820.jpg

It doesn’t get added… The exact same thing happens when I comment the HitMesh->Destroy(); code

Oh my god… I’m just idiot! It works just fine… just added some debugging code…



if (Inventory.Num() >= 1)
	{
		GEngine->AddOnScreenDebugMessage(12, 1.0f, FColor::White, TEXT("ItemName: ") + Inventory[0]->Name.ToString());
	}


And I get the item’s name :smiley: sorry for that

No problem and thats the way to debug!