Announcement

Collapse
No announcement yet.

Pickup item with raycasting doesn't work..

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:

    Code:
    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:
    Click image for larger version

Name:	44f3044401.jpg
Views:	1
Size:	313.1 KB
ID:	1142386

    So any ideas?
    Raise the Black Flag!

    - [TUT]: HTTP GET Request in C++
    - [Tool]: Game Site Tracker website for your project
    - [Website]: Website for my game Leviathan

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

    Click image for larger version

Name:	preset.png
Views:	1
Size:	24.0 KB
ID:	1064659

    Edit:
    Code:
    USphereComponent * MySphere;
    MySphere->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
    Last edited by Zhi Kang Shao; 12-29-2014, 04:58 PM.
    Journeyman's Minimap - Available on Marketplace - Forum topic - Video

    Comment


      #3
      Nope I didn't know I needed to do that... let me try to add it.
      Raise the Black Flag!

      - [TUT]: HTTP GET Request in C++
      - [Tool]: Game Site Tracker website for your project
      - [Website]: Website for my game Leviathan

      Comment


        #4
        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!
        Raise the Black Flag!

        - [TUT]: HTTP GET Request in C++
        - [Tool]: Game Site Tracker website for your project
        - [Website]: Website for my game Leviathan

        Comment


          #5
          Glad it works, you're welcome!
          Journeyman's Minimap - Available on Marketplace - Forum topic - Video

          Comment


            #6
            A new problem occurs...

            Now the casting works perfectly fine but I do this:

            Code:
            //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?
            Raise the Black Flag!

            - [TUT]: HTTP GET Request in C++
            - [Tool]: Game Site Tracker website for your project
            - [Website]: Website for my game Leviathan

            Comment


              #7
              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?
              Journeyman's Minimap - Available on Marketplace - Forum topic - Video

              Comment


                #8
                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:




                It doesn't get added... The exact same thing happens when I comment the HitMesh->Destroy(); code
                Raise the Black Flag!

                - [TUT]: HTTP GET Request in C++
                - [Tool]: Game Site Tracker website for your project
                - [Website]: Website for my game Leviathan

                Comment


                  #9
                  Oh my god.. I'm just idiot! It works just fine..... just added some debugging code...

                  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 sorry for that
                  Raise the Black Flag!

                  - [TUT]: HTTP GET Request in C++
                  - [Tool]: Game Site Tracker website for your project
                  - [Website]: Website for my game Leviathan

                  Comment


                    #10
                    No problem and thats the way to debug!
                    Journeyman's Minimap - Available on Marketplace - Forum topic - Video

                    Comment

                    Working...
                    X