• 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 dfghdh · Jan 21, 2013 at 03:35 PM · javascriptraycastfpsraycastingprojectile

Question about FPS and Raycasting Javascript

I am making a weapon system for a FPS (not the whole game) that controls everything to do with weapons such as picking up, switching, animations etc. I am currently working on the firing and I am firing a circular "bullet" and then testing its collider for if and who it hits. Is this the best way to simulate weapon fire? The more I look around I see a lot of people using ray casting and wondered what the benefits/drawbacks of doing this are. Bear in mind that I am not just working on one type of weapon so I need to know could the same technique be used for weapons with a considerable amount of bullet drop (sniper rifles/grenade launchers and mortars) or is it exclusive to weapons where the precision of a realistic simulation isn't vital (such as small arms and automatic weapons)

Comment
Meater6

People who like this

1 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 Meater6 · Jan 21, 2013 at 03:53 PM 1
Share

After looking at your profile, and saw your questions, I saw most of them were answered decently, but you never accepted them. May I ask why?

avatar image dfghdh · Jan 21, 2013 at 06:52 PM 1
Share

To be completely honest I didnlt know I could do that. I am reasonably new to using forums.

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Meater6 · Jan 21, 2013 at 03:50 PM

With grenade launchers, rocket launchers, etc. I would instantiate an object and use it as the projectile. This makes it more realistic. For weapons like assault rifles, shotguns, and (usually) sniper rifles, I would use a raycast.

If you want a real (as in, gravity, air velocity, drag, travel time, etc.) for weapons with a very fast initial fire velocity, you can still use raycasts, but create it with segments. The first raycast starts at the gun barrel, and moves straight (or whatever direction your pointing at) for speed * Time.deltaTime. Then the next frame calculate gravity, air velocity and drag, that would alter the course of the bullet for over 1 frame, and calculate the new direction and length of the ray. Then cast the ray from the end of the last raycast. Repeat.

I make it sound hard, but its really not.

Hope this helps. :)

Comment
drizztmainsword
AlucardJay
siphon

People who like this

3 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 Meater6 · Jan 21, 2013 at 06:48 PM 0
Share

Your absolutely right. You should convert it to an answer. :)

avatar image Meater6 · Jan 21, 2013 at 06:48 PM 0
Share

Oh, but wait, you'll lose your upvotes. Well, if you do convert it, I'll re-upvote it. Promise. ;)

avatar image dfghdh · Jan 21, 2013 at 07:03 PM 0
Share

Thanks, I think I will do that with the sniper rifle shots. The other weapons don't need that level of realism. Can I make a "trail" for the shot to give some sort of visual feedback to the player in the case of the sniper rifle? They may want to see where the bullet is dropping to to adjust their aim accordingly. As for the grenade launcher I have created the weapon classes so that the same function is called regardless of the weapon so I can change the code inside the grenade launchers class separately.

avatar image Meater6 · Jan 22, 2013 at 04:17 PM 0
Share

There is the line renderer. And there's the trial renderer. They are ideal for what you just mentioned. Then there's particle too,they can also work if that's what your looking for.

Line Renderer: http://docs.unity3d.com/Documentation/Components/class-LineRenderer.html

Trail Renderer: http://docs.unity3d.com/Documentation/Components/class-TrailRenderer.html

avatar image

Answer by sparkzbarca · Jan 21, 2013 at 05:43 PM

the important things to note are.

Raycasts are way less intensive than an actual object and there is no getting around that. a 1000 bullets firing vs 1000 raycasts, you want the raycasts.

Second bullet travel REALLY FAST. So fast in fact that basically trying to simulate them with actual objects wouldn't work. See each frame update a object has to be touching or be inside an object for it to realize it's colliding and start calculations to make everything play out right. BUT if you go to fast you can actually pass through objects. If the width of the object and your width is small enough, and your speed is high enough, you can pass through in less than frame. The collision may never occur.

Bullets have a really small width, they travel very fast, they may not get detected hitting objects which are narrow at the point of impact. You obviously don't want that. So you pretty much have to use raycasts because they dont fail. (there is another option by the way, you can increase the frequency of physics steps, if you check every 1/120th of a second instead of 1/60th you can catch an object going twice as fast, but then you are doing twice as many physics checks a second and that can get really intensive really fast)

Comment
drizztmainsword
AlucardJay
Meater6

People who like this

3 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Rotate RayCast/Object right on instantiation? 1 Answer

Raycast script help? 1 Answer

Raycast object as far as possible? 1 Answer

Launching a Projectile to a Raycast 1 Answer

(Raycasting) Changing Position 0 Answers


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