Block dragged object

Hello guys
I’m creating a software where objects dragged by the mouse move and connect to each other via sockets.

How can I prevent these objects from intersecting each other and above all how can I prevent these objects from intersecting, for example, walls within the game space?

what I would like is that if these objects are dragged onto a wall they freeze, as if there is a collision.

many thanks to all

Both objects that are colliding could have a box collision component to detect if they are touching.

Then, in the code of the object that is moving you can subscribe to the OnComponentHit delegate and call a function that you make called OnCollision:

CollisionBox->OnComponentHit.AddDynamic(this, &YourMovingObject::OnCollision);

When the OnCollision function is called, you can check what you hit and also perform some logic to freeze the moving object. For example:

void YourMovingObject::OnCollision(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)

{
    if (OtherActor->ActorHasTag("Wall") && !frozen)
    {
        // If the other actor has the "Wall" tag, freeze the object
        frozen = true;
        StopMovingAndFreeze(); // Your code to stop stuff here
    }
}

Note that how you actually freeze your object will depend on how you moved it in the first place.

1 Like

I really appreciate your help, but unfortunately I use node-based blueprints

Could you give me an example with nodes?
I would be really grateful

2 Likes

Many thanks
I have don this, I set “hit simulation” and “gravity” to the hit box, I set the tag on wall actor and add a “print string” to show me the result, but nothing…

Wall actor have a “root component” and a “static mesh”, I set the tag to both element

many thanks for your help

It’s hard to understand how things interact if we can’t see the whole picture. A screenshot of the Details panel of the moving object and the wall would help.

There are actor tags and component tags. Which one did you set? In this case, only the actor tag of the wall needs to be set.

Try adding a debug statement after the On Component Hit node to see if the issue is with the event being triggered, or if it’s something further down the line in the logic.

1 Like

You also need to check “simulation generates hit events” in the details panel of your component.

1 Like

Thanks, my problem was that I had to put the collision box as parent of the mesh, now the hit event works.

my problem now is to find a function that freezes the movement of the object dragged by the mouse when a hit event happens

yes thanks, i did

@3dvisual.it Could you show me how is your object being dragged around by the mouse? I could then try to add a function to stop further motion.

Also should these objects snap to the closest socket?

in “camera pawn”

and this is “drag Start”

Many Thanks

In how many axis is the block dragged? Only x and y? (1 plane)

Yes

DragBlock.zip (151.0 KB)

Dragging works with snapping prototype but the collision is not registering.

I’d probably move towards a physics object and using grab component to be able to keep the physics collision. Might find time later to implement it.