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.