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. link text
Answer by DonJorris · Aug 26, 2018 at 10:01 AM
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: https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html). 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(); )}}
I've read that that doesn't really work on fast moving projectiles because the collision detection isn't good enough and the rigidbody just goes through what it wants to hit frame to frame. $$anonymous$$aybe that's old advice?
Is continuous collision detection better than it used to be?
I took the ray out of the coroutine and put the whole script on the instantiated gameobject. If I put the drawray outside of the if (stepsize) then it works mostly as I would expect, but either inside of that, or if I remove that if statement and put it inside the if(physics.raycast) it doesn't work. $$anonymous$$s me to believe there is an issue with "stepsize" in the raycast function since the drawray doesn't need that length argument.
I think I'm going to shelve this and just use raycasts for now. I will pull this out and only use it on weapons with more interesting ballistics.
I was having performance issues when I really up'ed the firerate and ammo count.
Your answer
Follow this Question
Related Questions
Issue with Raycast hit.distance Variables Reseting Itself 0 Answers
Multiple objects being instantiated instead of one 0 Answers
Enemy is shooting the opposite direction of player 1 Answer
FPS Raycasting vs Instantiate 0 Answers
Raycast inconsistent 1 Answer