I have few collision primitives assigned to my static mesh.
Each collision primitve has a ‘name’ property (static mesh details->collision->primitives->boxes->0->name)
During ‘Event Hit’ I would like to get a name of primitive that was involved in this collision.
Is this possible?
May be done in Blueprints od C++.
Hi, I need something exactly like this but for MyComp. I want to obtain the name of the primitive, the element that was hit (I have a bumper in my car and want to know which element I hit) . Is this possible?
I’ve implemented it this way, but it doesn’t seem to work. When I use your code in this case, I’m not sure how to get the element index for the hit component. I have three collision capsules — one for the front and two for the sides.
When I use my own function GetMyCollisionNameFromHit(), the Hit.MyItem is always -1. Could you help me figure out what’s going wrong?
.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CoreBlueprintFunctionLibrary.generated.h"
UCLASS()
class PROJECT_API UCoreBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
/** Returns the name of the collision element from the component based on the index. */
UFUNCTION(BlueprintCallable, Category = "Collision")
static FString GetCollisionName(UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION(BlueprintCallable, Category = "Collision")
static FString GetMyCollisionNameFromHit(UPrimitiveComponent* SelfComp, const FHitResult& Hit);
};
This is hard to calculate. Methinks it should just get the name of the primitive that struck. This element is not complicated, but I also have ones that consist of many.
The index is needed when multiple collisions are attached to the same static mesh, otherwise the code won’t recognize the correct collider. You have to pass in the index from the collision event based on the hit result’s element index.
If I want to detect what I hit, then yes. But if I want to find out what I hit (i.e. if the player had several collisions and hit one of the walls), I don’t have that index in BP. The index element returns the number of what I hit, but I want to know what element the player hit. MyItem in C++ always returns -1.
Hi, I need something exactly like this, but for MyComp. I’m trying to retrieve the name of the primitive and the specific element that was hit - for example, I have a bumper on my car and want to determine exactly which part was impacted. Is this possible?
That’s not a problem. I have added that header. FKShapeElem does not have the GetTransform function. I don’t know why it works for you. Maybe you have modified that element?
Only objects such as FKSphylElem have this function.
I understand, but this isn’t about detecting which object the player collided with — it’s about detecting which part of the player was involved in the collision.
Imagine the player is driving a car. The car hits a wall. I want to find out which component of the car was damaged by the collision. I’m not interested in which part of the wall was hit.
The OnHit event is inside the car Blueprint, because I’m not going to make every object the player might crash into into a separate Blueprint just to detect whether the collision was with the car’s front bumper or the side. That’s why I’m not interested in OtherComp. I care about MyComp — the component on the car that was hit.
So I don’t want to know HitResult.ElementIndex, which gives me the index of the hit element on the other object. I want to know the index of the car component that was hit.
Okay. I’ll try to explain it differently. The player is a car whose body is a Static Mesh. The collisions are made up of multiple primitives, each with a different name. When the car (player) drives into a wall, the OnHit event is triggered in the car (player). What I want to find out is which part of the car caused the collision.
In the example you sent, the check is done from the perspective of the wall that was hit. But what I need is to check from the player’s (car’s) side — I want to know which component of the car hit something.