• 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
Question by Disaster · Aug 13, 2011 at 11:50 AM · physicsraycastbullets

Bullet collision help

My bullets are occasionally flying through objects and I really can't figure out why. I'm using raycasting as opposed to colliders. Strangely, the speed of the bullet doesn't seem to stop this from happening, can anyone see anything wrong with my code?

 using UnityEngine;
 using System.Collections;
 
 public class BulletBase : MonoBehaviour {
     
     public const string BulletHit = "bullethit";
     public const string BulletExplode = "bulletExplode";
     
     public float speed  = 10.0F;
     public float lifeTime = 2.0F;
 
     protected Transform _tr;
     protected float _spawnTime = 0.0F;
     
     private void Awake() {
         _tr = transform;
     }
     
     public virtual void Start() {
         _tr = transform;
         _spawnTime = Time.time;
     }
     
     private void FixedUpdate() {
         // Check for collisions
         RaycastHit hit;
         if (Physics.Raycast(_tr.position, _tr.forward, out hit, 1.0F)){
             if(hit.transform){
                 Messenger<RaycastHit>.Broadcast(BulletBase.BulletHit, hit);
                 DestroyBullet();
                 return;
             }    
         }
     }
 
     private void Update() {
         _tr.position += _tr.forward * speed * Time.deltaTime;
     
         if (Time.time > _spawnTime + lifeTime){
             DestroyBullet();
         }
     }
 
     public void DestroyBullet(){
         ObjectPoolManager.DestroyPooled( gameObject );
         gameObject.SetActiveRecursively(false);    
     }
 }
Comment
Statement

People who like this

1 Show 0
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
Best Answer

Answer by Statement · Aug 13, 2011 at 11:59 AM

You're raycasting 1 unit ahead, but move them 10 per second. This is error prone. Try something like this:

 private void FixedUpdate() {
     // Check for collisions
     RaycastHit hit;
     float moved = speed * Time.deltaTime;     
     if (Physics.Raycast(_tr.position, _tr.forward, out hit, moved)){
         if(hit.transform){
             Messenger<RaycastHit>.Broadcast(BulletBase.BulletHit, hit);
             DestroyBullet();
             return;
         }   
     }
     // Moved Update code to FixedUpdate.
     _tr.position += _tr.forward * moved;
     if (Time.time > _spawnTime + lifeTime){
         DestroyBullet();
     }
 }

 private void Update() {
     // Nothing goes here
 }

Basically, move the update code to fixed update and raycast for as long down the line you intend to move it. FixedUpdate may happen more frequently than your Update so you should also move the transform in FixedUpdate.

Comment
Disaster

People who like this

1 Show 4 · 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 Statement · Aug 13, 2011 at 12:00 PM 0
Share

Oh and you might want to do the life time test in the beginning of the function, with an early return if it was destroyed.

avatar image Disaster · Aug 13, 2011 at 12:02 PM 1
Share

Argh, so obvious now I look at it! Thanks.

avatar image Statement · Aug 13, 2011 at 12:05 PM 0
Share

You've got clean code there, but I am curious as to why you get the transform in both Awake and Start? This is of course of little importance but I thought you want to tidy that up too. Nice code.

avatar image Disaster · Aug 13, 2011 at 12:09 PM 0
Share

Well this snippet is actually an amalgamation of two classes (which is why Start() is virtual) , I have a base bullet class and a sub class which is unique for each different bullet type. Since I am using the Object Pool, I need to reset the transform as the Start() method gets called when the bullet is resurrected. I could probably do without the Awake method though, thanks for noticing :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How To Really Do High Quality Bullet Physics like in AAA Titles 1 Answer

Raycast and colliders problem. Corners of two colliders 0 Answers

Rotating to match surface normal makes object change its direction 1 Answer

Raycast not working 2 Answers

Physics.Raycast issue | Ray not casting at certain angles? 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