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 /
avatar image
0
Question by kdpkke · Feb 09, 2017 at 04:07 PM · c#collisionraycastingbulletpooling

Using raycast for visible bullet collision (not oncollisionenter) in C#

Hello guys, i've got a problem with my game. Here's the (strange) scenario: enemies have got a box collider, but bullets MUST NOT have a collider. Bullets are slow, so they are visible when i press space to shoot. I want that the bullet will destroy if it collides with an enemy. I have done every function to make it work (like pooling the bullet object and deactivate it after x seconds), but I've got a problem with the raycast. I've got this attached to the bullet prefab:

 void OnEnable ()
     {
         rayInitialPos = new Ray (bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.forward);
         rayEndPos = gameObject.transform.position.z;
         if (Physics.Raycast (rayInitialPos, out hit,rayEndPos)) {
             if (hit.collider.tag == "Enemy") {
                 GameObject enemy = hit.collider.gameObject;
                 gameObject.SetActive (false);
                 //faccio i controlli per diminuire vita o distruggere oggetto enemy se morto
                 EnemyScript.TakeDamage (enemy);
             }
         } 
         else {
             Invoke ("Destroy", 4f);
         }
     }
 
     void Destroy(){
         gameObject.SetActive(false);
     }

The fact is that if Physics.Raycast (rayInitialPos, out hit,rayEndPos) it's true (so i'm in front of an enemy and i'm aiming it), the bullet is set immediately to False and I cannot see the bullet. My idea was to have the rayEndPos value exactly like the length of the bullet, and when the bullet hits the enemy, it disappear. How can I achieve that? Remember that I cannot use OnCollisionEnter and I cannot put a collider inside the bullet object for personal reasons. Thank you in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by PizzaPie · Feb 09, 2017 at 05:37 PM

The ray cast should be casted every frame then, and not only on the first one as you have it, put it on the Update() . Also i would change the raycast to physics.Raycast(gameObject.transform.position, gameObject.transform.position.forward, someRange, out hit) change the someRange to achieve the desired effect. May i ask why bullets MUST NOT have colliders? Constant raycasting will have a greater performance cost. Cheers

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

8 People are following this question.

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

Related Questions

How to check if a raycast is not hitting any tagged colliders? 0 Answers

Could some one help me with this script... 5 Answers

Distribute terrain in zones 3 Answers

What's a reliable way to detect if an object is no longer being hit by a ray? 1 Answer

Parent colliding bullet to the object 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