- Home /
Raycast not ignoring layers?
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, pls help.
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"); }
Make the code formatting better. Hardly understandable.
Answer by $$anonymous$$ · Apr 12, 2021 at 08:43 PM
Try this
Void fixedupdate ()
{
RaycastHit hit;
If (input. GetKeyDown (Keycode. Which button you want ))
{
if(Physics.Raycast(transform.position, transform.Forward , out hit, your range, player layer ))
{
//Do what you want
}
}
}
Answer by undevable · Apr 12, 2021 at 08:55 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. Hope this helps!
Follow this Question
Related Questions
Why wont Raycast wont ignore layer? 1 Answer
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
Animating an automatic door 0 Answers