How do i stop MyPawn from moving through walls in my game?

Hello, i am currently creating a game which is a maze game and requires the player to move to a certain area where an overlap then occurs with a trigger box which ends the level. I am using a robot as a controller for MyPawn (a sphere) because the purpose of this game is for stroke rehabilitation.

I have two C++ classes, one called Control which receives the x and y coordinates from the robot through a winsock socket. The second class is MyPawn class and here i have set the location of the sphere to the location of the robotic device for every tick in the game. Essentially the robot controls the pawn location.

The game works well except for the fact that the pawn can move through walls which defeats the purpose of the game. I was wondering if anybody could tell me whether i have to set the details of the walls to a specific setting or whether i have to alter my C++ classes to have the pawn collide with the walls.

I have added the Control and MyPawn cpp and the MyPawn header file below in the comments. Thanks for any help.

Hi HManProject,

assuming your collisions are set up properly your issue probably lies in this line:

SetActorLocationAndRotation(NewLocation, NewRotation);

If you have a look at the documentation, you’ll see there’s a bSweep parameter that determines whether collisions are triggered on the way. This parameter is set by default to false (from Actor.h):

bool SetActorLocationAndRotation(FVector NewLocation, FRotator NewRotation, bool bSweep=false, FHitResult* OutSweepHitResult=nullptr, ETeleportType Teleport = ETeleportType::None);

You want to make sure that’s set to true, so call the function as such:

SetActorLocationAndRotation(NewLocation, NewRotation, true);

Also worth mentioning “moving” an actor using SetActorLocationAndRotation (basically a teleport) is not advisable. I do recommend to have a look at the movement component (if you are using the DefaultPawn that’s already implemented), or, if it makes sense, use physics for this (adding impulses and forces that will cause your pawn to move).

Best,
f

1 Like

Hi @Fraps2020 , thanks for your help that has worked perfectly.

However, since collisions are present and my sphere can collide with the walls it is now pushing the walls out of the way and i was wondering if you knew how to fix this? Thanks.

Sounds like your walls are sets to movable and to simulate physics…

If that’s indeed the case, uncheck their “simulate physics” option and set their mobility to static.

Cheers
f

Thanks for your reply.

I have done this and now my sphere is moving through the walls again so i spoke too soon about setting the boolean to true. Do you have any idea how to fix this?

maybe look over the collision settings on your wall, make sure collision is on and set on “Block All”