P
P
PsyDuckbest2021-09-18 18:09:03
C++ / C#
PsyDuckbest, 2021-09-18 18:09:03

Why doesn't Raycast work?

Hello. In general, this script seems to work, but for some reason it only works when the player object does not move and only once, the function is called via Update, but it does not work every frame. Don't know what could be wrong?

void Shoot() {
        RaycastHit2D hit = Physics2D.Raycast(gun.transform.position, gun.transform.right, range, layerMask:5);
        Debug.DrawRay(gun.transform.position, gun.transform.right * range, Color.green);
            if(hit.transform.gameObject.CompareTag("Player")) {
                Debug.Log("hited " + hit.collider.name);
            }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question