Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Apr 28, 2014 at 05:24 AM by Benproductions1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jjmontes · Apr 21, 2014 at 10:32 PM · collisionraycastoptimizationbullet

Optimization of bullet hit calculation per frame

My bullet objects travel during several seconds.

I was originally calculating the hit point and generating a Bounds object so I could easilly tell when the bullet had impacted on the surface. This accounts for the bullet final impact point.

However, I need to calculate whether a bullet impacts on a game entity every frame. For this I am now casting a ray every frame from the curret position to "newPosition":

// Check if the bullet hit a player this frame var hitInfo : RaycastHit; if (Physics.Raycast(transform.position, bulletSpeed.normalized, hitInfo, (newPosition-transform.position).magnitude, 1<<8 )) { if (hitInfo.collider.tag == "Player") { // The bullet hit a player entity } }

Is there any way I can optimize this (perhaps trying a sphere collision before casting the ray), or is a "raycast per bullet per frame" the quickest I can come up with?

(My game uses physics but I am not using Rigidbody for bullets as that is noticeably slow).

Comment
Add comment · Show 2
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 Benproductions1 · Apr 22, 2014 at 08:08 AM 0
Share

Depending on the speed of your bullet, your might get away with just casting one ray at the start and moving towards the hit point. You could also do periodic raycasts ins$$anonymous$$d of one every frame.

avatar image jjmontes · Apr 28, 2014 at 02:10 AM 0
Share

Thanks. That is what I was doing, but my bullets travel over time (they live up to several seconds). I answered my own question below.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by jjmontes · Apr 28, 2014 at 02:24 AM

I ended up doing:

If gravity is applied, I cast 1 short raycast per bullet per FixedUpdate.

If no gravity is applied, I precalculate the final hit point but still cast a ray per bullet per frame, in this case including only the "players" layer:

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

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Destroy Turret with machine Gun 0 Answers

Bullet Shoot with RayCast don't destroy onCollisionEnter 1 Answer

Bullet Effect 2 Answers

Unity3 CollisionDetection.Continuous for Bullets causing ricochet 2 Answers

RayCast Problem : 2d 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