Bullets with raycast and physics

I’m loosely following a Brackeys’ tutorial (actually getting more and more loose, FPS) and I’m sorta stuck.

I didn’t really like the way he did shooting, so I’ve changed it significantly. Overall goal is to instantiate a rigidbody, shoot it out with some force, have it shoot a raycast the distance it will travel between frames for hit detection. In case the question comes up, the reason why I’m not just doing raycast is that 1) I’m mostly just doing this to learn 2) I anticipate having multiple projectile types, most of which probably won’t work with simple raycasts.

My code right now does all of those things except it won’t continuously cast rays. Originally I had it in the Shoot method, but it wasn’t getting called continuously. Then I put it into a coroutine, but I’m pretty new so I’m not sure how to construct that. The coroutine works, but it only casts one ray. Probably because I’m not returning what I need to return in order to have to run again until collision is detected.
EDIT: I moved the collision detection to a separate script that is on the bullet gameobject. I can raycast outside of both of the if statements using the same arguments that are in the if{Physics.raycast}, but if I go inside the if statements it doesn’t work.

I can’t figure out what’s causing the if statements not to work.

On top of that, I’m not 100% sure on the math for the raycast if/else function.

Interestingly, the DrawRay code is inside the if(raycast) but so is a debug.log that should return the name of the collider that was hit, but nothing is being returned.

I attached all the of the code in a .txt file.
[123365-fullscript.txt|123365]

The IEnumerator is called only once, because you are saying StartCoroutine(Bulletray()) when void Shoot() is called; That is why the ray is shot only once, when you press the “Fire1” button.
If you are using rigidbodies on your bullets, you can attach a script to the bulletprefab, which uses the void OnCollisionEnter() function to check if the bullet hit something. In that case you won’t need raycasts or IEnumerator (Here is a link for OnCollisionEnter: Unity - Scripting API: Collider.OnCollisionEnter(Collision)). Only use your PlayerShoot script to instantiate the bullet, like you already do, and let your bullet take care of damage and collision detection.

Your bullet script can look like this: (Not tested) public calss bullet : MonoBehaviour { void OnCollisonEnter(Collision col){ if(col.collider.gameObject.tag == "Enemy") col.collider.gameObject.GetComponent().GetDamage(); )}}

Updating to respond instead of answer.