Raycast not hitting on target?

Hello guys,

I’m writing part of the player’s ability to do combat, melee. But I don’t think the Raycast hits, nor does the AttackSequence enter the Hit function.

using System;
using System.Collections;
using System.Linq;
using System.Text;
using UnityEngine;

class PlayerAttack : MonoBehaviour
{
    private float lastAttack;
    public float attackCooldown = 1.5f;
    private PlayerController controller;
    public LayerMask hitLayers = -1;

    public void Awake()
    {
        controller = GetComponent<PlayerController>();
    }

    public void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            if (Time.time > lastAttack + attackCooldown)
            {
                lastAttack = Time.time;

                StartCoroutine(AttackSequence());
            }
        }
    }

    private IEnumerator AttackSequence()
    {
        animation.Play("attack");
        float animationDone = Time.time + animation["attack"].length;

        //controller.isControllable = false;

        float next = Time.time + 0.25f;
        while (Time.time < next) yield return null;

        Hit();

        while (Time.time < animationDone) yield return null;

        //controller.isControllable = true;
        animation.Play("idle");
    }

    private void Hit()
    {
        RaycastHit hit;
        
        if (Physics.Raycast(transform.position + Vector3.up, transform.forward, out hit, 3, hitLayers.value))
        {
            Hitable target = hit.collider.GetComponent<Hitable>();
            if (target)
            {
                target.Hit(20);
                Debug.Log("Player has attacked target.");
            }
        }
    }
}

public class Hitable : MonoBehaviour
{
    float hitpoints = 100;
    public void Hit(float damage)
    {
        hitpoints -= damage;
        if (hitpoints <= 0)
            Debug.Log("Enemy dead.");
    }

}

Thanks in advance,

Tolga.

The code seems ok, but many things may go wrong:

1- You’re casting a forward ray from transform.position + Vector3.up; if the character is 2 units tall, the raycast will start from to top of its head, possibly passing over the enemy (unless the enemy is a big guy). Try to start the ray at transform.position instead.

2- Since you’re using a mask, be sure that it includes the layer where the enemy is. This mask is initialized to -1 in your code (all layers), but may have been changed in the Inspector, and this bastard has the memory of an elephant! I would simply remove the mask parameter until everything is working 100%.

3- The script Hitable may not be attached to the enemy, or may be attached to a children or parent of the hit collider - in any of these cases, GetComponent(Hitable) would return false and the damage would not be applied. Place a debug line in the if:

  if (target){
      target.Hit(20); // enemy hit
      Debug.Log("Player has attacked target.");
  } else {
      Debug.Log("No Hitable"); // no Hitable script found
  }