Help Needed! Why is my raycast not working?

I get no errors, but nothing happens when I press space.
Any more info needed can be provided.
Thanks.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour
{
public float attackRange = 5f;
public float attackDamage = 1f;
int layerMask = 1 << 9;

void FixedUpdate()
{
AttackRaycast();
}

private void AttackRaycast()
{
//left
if (gameObject.GetComponent().playerFacingX == -1 && Input.GetKeyDown(KeyCode.Space))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.left), attackRange, layerMask);

if (hit)
{
Debug.Log(“HIT”);
}
}

//right
else if (gameObject.GetComponent().playerFacingX == 1 && Input.GetKeyDown(KeyCode.Space))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), attackRange, layerMask);

if (hit)
{
Debug.Log(“HIT”);
}
}

//down
else if (gameObject.GetComponent().playerFacingY == -1 && Input.GetKeyDown(KeyCode.Space))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.down), attackRange, layerMask);

if (hit)
{
Debug.Log(“HIT”);
}
}

//up
else if (gameObject.GetComponent().playerFacingY == 1 && Input.GetKeyDown(KeyCode.Space))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.up), attackRange, layerMask);

if (hit)
{
Debug.Log(“HIT”);
}
}
}
}

First off, try to put your code in a code box so it’s easier to read:

Like this.

There is a button on the text tools for that purpose, it looks like a document with <> inside it.

Secondly, I am not sure what is wrong but I do think your code could be a lot better if you first tested if the player pressed space and then tested the direction, instead of a chain of else ifs, like this:

if (Input.GetKeyDown(KeyCode.Space)
{

//left
if (gameObject.GetComponent<PlayerMovement>().playerFacingX == -1)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.left), attackRange, layerMask);

if (hit)
{
Debug.Log("HIT");
}
}

//right
if (gameObject.GetComponent<PlayerMovement>().playerFacingX == 1)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), attackRange, layerMask);

if (hit)
{
Debug.Log("HIT");
}
}

//And so on...
}

This makes it easier to read and might fix the problem as it’s likely the ifs are not returning true.

Please use code tags: Using code tags properly

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.