- Home /
Why wont Raycast wont ignore layer?
I made a damage system so when an enemy collides with the raycast and you click it damages the enemy. but every time I put my sword on the player the damage system stops working and I tried to make the raycast ignore the sword but it won't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyChecker : MonoBehaviour
{
[SerializeField] private float range;
public LayerMask sword;
public GameObject enemy;
private bool canSlash;
RaycastHit hit;
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Vector3 back = transform.TransformDirection(Vector3.back);
Vector3 dwn = transform.TransformDirection(Vector3.down);
Vector3 up = transform.TransformDirection(Vector3.up);
Debug.DrawRay(transform.position, fwd * range, Color.green);
if (Physics.Raycast(transform.position, fwd,out hit, range -sword)&&hit.transform.tag == "Enemy")
{
if (Input.GetMouseButtonDown(0))
{
enemy.GetComponent<EnemyHealth>().TakeEDamage(5);
}
print("ObjectLocked");
}
}
}
Answer by undevable · Apr 12, 2021 at 09:03 PM
On your sword, go to the Inspector tab. From there, go on the layers dropdown, and select the layer, "Ignore Raycast". This will make the Raycast ignore the sword. Much easier to implement rather than coding it all yourself. Hope this helps!
Your answer
Follow this Question
Related Questions
Raycast not ignoring layers? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Detecting that I'm clicking a unit even though I'm not? 0 Answers
Stop the movement to a direction 0 Answers