- Home /
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.
Answer by aldonaletto · Oct 16, 2012 at 02:53 PM
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
}
Thanks for the advice, and help. :)
I tried checking it with the debug logs, and then tried changing the line just above if (target).
I figured out why it didn't register any hits on the object I was testing it on. I adjusted this line:
Hitable target = hit.collider.GetComponent();
to this:
Hitable target = hit.collider.gameObject.transform.GetComponent();
Now all hits and damages are working!
Regards,
Tolga.
Your answer
Follow this Question
Related Questions
How do I use layermasks? 9 Answers
Layer mask doesn't work... 1 Answer
Why is Raycast ignoring the layer mask? 1 Answer
Detecting that I'm clicking a unit even though I'm not? 0 Answers
Layer Mask Detection 2 Answers