Destroying game object next to player

Hi i am trying to create a script that destroys a game object that is directly next to my player with a keyboard command.

My game has a 2d perspective and a third person view with the camera locked to following my player who is controlled by a character controller ( i guess similar to old mario games) what i am aiming for is - by pushing a direction (left, up, right or down) and the “k” key you destroy the game object adjacent to the player in that direction

the current script i have is destroying game objects when i click but i can’t figure out how to get it to work the way i want. I have tried changing the input method to (for eg) “K” but it still has no effect

the code i have is

function Update(){
  if (Input.GetMouseButtonDown(1)){
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){
      Destroy(hit.transform.gameObject);
    }
  }
}

Any help would be greatly appriciated

Can’t you use the player characters location as source for your ray? Something like:

if (rightButton) {
   var ray = new Ray ( player.transform.position, Vector3.right, distance);
} else if (downButton) {
   var ray = new Ray ( player.transform.position, -Vector3.up, distance);
}
// Same for left (-Vector3.right) and up (Vector3.up)

var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){
   Destroy(hit.transform.gameObject);
}

This is very untested, but something along these lines should work.