Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xrenuix · Aug 26, 2018 at 02:46 AM · instantiateraycastcoroutinehit detectionfps tutorial

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

fullscript.txt (15.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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(); )}}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xrenuix · Aug 26, 2018 at 03:00 PM 0
Share

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.

avatar image xrenuix · Aug 26, 2018 at 03:14 PM 0
Share

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.

avatar image
0

Answer by xrenuix · Aug 26, 2018 at 02:58 PM

Updating to respond instead of answer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges