unity – Teleport character a fixed distance in the direction it is looking

Game : bird’s eye view (2D)

  1. When I touch the button, my character will teleport a fixed distance in the direction it is facing.

  2. If there is an object at that distance (like a wall) it will not teleport (it can pass through the wall)

For example, the code below teleports the character, but only takes it to the position determined on the stage with the mouse.
I also tried the “raycast” system, but the problem is that even if there is an object nearby, the character should be able to teleport from there, it does not provide me with raycast. (because of Raycasthit)

Actually what I think is very simple. Assigning the transform of the point in the direction my character is facing, for example 10 units away, to my character’s transform. This way my character will be teleported. However, of course, if there is a collider 10 units away, it cannot teleport, but on the other hand, my character can teleport even if there is a collider object 6 units away. Although it seems simple, I could not do it.

void Update ()
   if( Input.GetMouseButtonDown( 0 ) )
    transform.position = Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, 10 ) );