- Home /
 
Raycast not detecting a hit from far away, it only detects when close.
Hello, I am new to gamedev and I am trying to learn, unity, and coding (I am trying c#), this is also my first post so I will describe my issue the best I can. I have been stuck on this problem for a long time now (about 2 weeks). I have a 3d game that I have been working on and ran into an issue where the raycast wont hit the enemy from far away (I have a debug.log telling me if I hit the enemy or missed), it only hits the enemy when he is really close to the player (about 1 unit maybe). I have spent my time researching and experimenting and cant find a fix. This problem started when I re-worked the way the ragdoll instantiates when the enemy dies, (this fixed issue with the ragdoll making it work just fine). I have re-worked the way the enemy follows the player to be a rigidbody movement to work with physics, but this didn't fix it. I re-worked the raycast to come from the camera, but this didn't fix it. The enemy does has a capsule collider and also a rigidbody. Note I know EnemyHealth says set collider state to false but if I change it to true it still wont fix it. Any help will super helpful, Thankyou
Here is my code
AIController
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class AIController : MonoBehaviour { public NavMeshAgent enemy; public Transform Player; [SerializeField] private float attackDamage = 1f; [SerializeField] private float attackSpeed = 1f; public float movementSpeed = 1f; private float canAttack; [SerializeField] float stoppingDistance;
 void Start()
 {
     Player = GameObject.FindWithTag("Player").transform;
 }
 void FixedUpdate()
 {
     Vector3 direction = (Player.transform.position - transform.position).normalized;
     GetComponent<Rigidbody>().MovePosition(transform.position + direction * movementSpeed * Time.deltaTime);
 }
 
 void Update()
 {
     enemy.isStopped = false;
     if (enemy.remainingDistance > stoppingDistance)
     {
         GetComponent<Animator>().SetTrigger("Chase");
     }
     if (enemy.remainingDistance <= stoppingDistance)
     {
         GetComponent<Animator>().SetTrigger("Attack");
         StopEnemy();
     }
 }
 private void StopEnemy()
 {
     enemy.isStopped = true;
 }
 private void OnTriggerStay(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         if (attackSpeed <= canAttack)
         {
             Debug.Log("Damage");
             other.gameObject.GetComponent<PlayerHealth>().UpdateHealth(-attackDamage);
             canAttack = 0f;
         }
         else
         {
             canAttack += Time.deltaTime;
         }
     }
 }
 
               }
EnemyHealth
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyHealth : MonoBehaviour { public float enemyHealth = 100f; public GameObject ragdoll;
 void Start()
 {
     setRigidbodyState(true);
     setColliderState(false);
 }
 public void DeductHealth(float deductHealth)
 {
     enemyHealth -= deductHealth;
     if (enemyHealth <= 0) { EnemyDead(); }
 }
 public void EnemyDead()
 {
     Destroy(gameObject, 3f);
     GetComponent<Animator>().enabled = false;
     setRigidbodyState(false);
     setColliderState(true);
 }
 void setRigidbodyState(bool state)
 {
     Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
     foreach(Rigidbody rigidbody in rigidbodies)
     {
         rigidbody.isKinematic = state;
     }
     GetComponent<Rigidbody>().isKinematic = !state;
 }
 void setColliderState(bool state)
 {
     Collider[] colliders = GetComponentsInChildren<Collider>();
     foreach (Collider collider in colliders)
     {
         collider.enabled = state;
     }
     GetComponent<Collider>().enabled = !state;
 }
 
               }
AK (for the gun/raycast)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AK : MonoBehaviour { Ray ray; RaycastHit hit;
 //Used to damage enemy
 [SerializeField]
 float damageEnemy = 0;
 [SerializeField]
 int currentAmmo;
 //Rate Of Fire
 [SerializeField]
 float rateOfFire;
 float nextFire = 0;
 [SerializeField]
 float weaponRange;
 void Update()
 {
     if(Input.GetButton("Fire1")&& currentAmmo >0)
     {
         Shoot();
     }
 }
 void Shoot()
 {
     if(Time.time > nextFire)
     {
         nextFire = Time.time + rateOfFire;
         currentAmmo--;
         ray = Camera.main.ScreenPointToRay(transform.position);
         if (Physics.Raycast(ray, out hit, weaponRange))
         { 
             if(hit.transform.tag == "Enemy") 
             {
                 Debug.Log("Hit Enemy");
                 EnemyHealth enemyHealthScript = hit.transform.GetComponent<EnemyHealth>();
                 enemyHealthScript.DeductHealth(damageEnemy);
             }
             else 
             {
                 Debug.Log("Missed");
             }
         }
     }
 }
 
               }
I didn't dig too far into the details, but the most obvious question comes to $$anonymous$$d: Have you tried increasing the weaponRange in the AK class?
Yes, I had it at 50 and thought the problem might be the range so I increased it to farther ranges but it still didnt work.
Your answer
 
             Follow this Question
Related Questions
How can I make a raycast move towards an object? 1 Answer
ray cast not working ? 2 Answers
Raycast From Within An Object 1 Answer
Raycast causes all enemies to attack 1 Answer
How to check if an empty GameObject is inside an object? 2 Answers