When the player hits (enters the collider of) the wall, does the speed == 0
and does the player stop moving?
If this is the case, then the problem is that you're setting the player speed
to 0 if they're in the wall, but because their speed is now 0 there's no way for them to escape the wall.
Solutions
There's many ways you could go about solving this, here are some options.
Unity Physics System
The simplest and probably most robust solution, unless you have very specific needs or need a lot of control, is just to use Unity's Physics system. You would add physics colliders to the walls and player (along with corresponding rigidbodies).
In your code, you would replace all instances of transform.position = target
with myRigidBody.MovePosition(target)
.
Vector3 target = Vector3.MoveTowards(transform.position, destination, Time.deltaTime * speed);
//instead of this
//transform.position = target;
//use this
myPlayerRigidBody.MovePosition = target
RigidBody.MovePosition(Vector3 v)
will automatically resolve collisions for you, so rigidbodies will never overlap. You could also just change the rigidbody's velocity, instead of using your own speed
variable, but then you would have less control over the movement.
Custom
The other solution(s), is to perform your own intersection/resolution logic. This can be done in countless ways, with varying levels of robustness and complexity (eventually you could end up creating your own custom Physics system).
The minimum viable behavior I'd start with, would be preemptively check if a destination or target is within a wall (this could be achieved using Physics.OverlapSphere
): if it is overlapping, don't move that frame and set your speed to 0; if there is no overlap, then move to the destination and keep whatever speed you have.
This is a huge topic though, and the complexity will vary with what type of movement (3D/2D) and colliders you want to support.