I'm stuck on Challenge "hide behind the wall".

As the title says. I managed the first two but am stuck on this one.

I know it has something to do with this below.

func _physics_process(delta:float) -> void:
     if _target:
  if rayCast.get_collider() != _target:
    pass # Do nothing, stand by...

But not sure to achieve the pseudo-coded part.

My general idea was to set the _target to "null" if the collided object was not the robot. This sort of works but overall not. It gives me a little bug that the shield can't detect the robot unless the robot gets out of the detection area first since the _target valuable is still "null" until then.

My other idea was to check the object the raycast collided with and set the layermask to a different one but after all, I think I'm having the same problem as the one above.

I reviewed the "bat" lesson once again thoroughly. The difference between this one and the bat lesson is Vector2.UP; The bat will automatically try to find the ceiling once the raycast can't detect the robot object. Therefore,  _target will automatically reset once the bat gets far enough from the robot. This one is top-down so it doesn't happen. I thought "what if I wrote 'do nothing' code...?" but then, I couldn't come up with a solid idea for it.

I even sat on it overnight and came back to it with a fresh set of my eyes but nope, haven't had the light-bulb moment yet. lol

Maybe this is a dumb question. lol I feel like I'm missing something big but very basic. Would you be able to help me with this, please?

  • Nathan Lovato replied

    You're pretty close to it, seeing how you approached this, you just don't have to set the _target to null. You could have logic like this:

    func _physics_process(delta:float) -> void:
         if _target:

              if rayCast.get_collider() != _target:
                  return # Leave the function so the shield does nothing
                  # Nest all the attack logic in here

    I only have a short code snippet here so I might be lacking context about your code but this would be the general idea. You don´t have to mess with the target variable: you can consider that the shield has a target in range but it doesn't have a clear line of sight to it.

    This will be simpler than trying to clear the target variable.

    1 love
  • q
    qyoneda replied

    Thank you, I think I was able to make it work with this!

    It's funny, it's always one little thing. Once you see it, the answer is always just so simple...