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 /
avatar image
1
Question by drift501 · May 20, 2012 at 10:18 AM · physicsrigidbodyraycastingsimulation

Shooting with raycasting or physics?

Hello. I am curremtly making a game that needs realistic shooting. Is it better to do this with raycasting or rigidbody physics attached to a bullet object. I have heard in places that rigidbodied bullets can go through things but they are more realistic. I would prefer to use rigidbody physics as well because I have knowledge on them. Is the rigidbody going through objects a problem and if it is how can I solve it? Thanks, drift501

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 Estevominador · Jun 15, 2012 at 11:49 AM 0
Share

so, you guys are saying, to make a realistic shooting, has to be an rigidbody and a raycast from the rigidbody?

avatar image Bunny83 · Jun 15, 2012 at 12:02 PM 0
Share

It depends what you want to simulate. For example Quake3 (or QuakeLive) has a lot of weapons. Some are instant hit weapons (machine gun, railgun, shotgun, lightning), some are projectile weapons (all others like rocket launcher, plasma, grenade).

Instant hit weapons are always using a raycast. Projectile-weapons use a real object that moves from the shooter towards the target.

In any case it's recommended to use a Raycast, but in a different way. Instant hit weapons cast a ray with a length of your weapons max range value. projectile weapons only cast a ray ahead their next movement step. See DontGoThroughThings

Btw. There are cases where you might think it's a projectile, but it isn't ;) In Unreal (the old one) the shockrifle's primary attack looks like a projectile, but it is an instant hit shot. It's just an animation.

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by aldonaletto · May 20, 2012 at 01:18 PM

You can reasonably simulate a real shot with raycasting: do a first exploratory raycast when the fire button is pressed; based on the hit.distance, calculate the drop and flight time, wait for the flight time delay and do the actual raycast shot taking the drop into account - like this:

var bulletSpeed: float = 1000; // bullet speed in meters/second var shotSound: AudioClip;

function Fire(){ if (audio) audio.PlayOneShot(shotSound); // the sound plays immediately var hit: RaycastHit; // do the exploratory raycast first: if (Physics.Raycast(transform.position, transform.forward, hit)){ var delay = hit.distance / bulletSpeed; // calculate the flight time var hitPt = hit.point; hitPt.y -= delay * 9.8; // calculate the bullet drop at the target var dir = hitPt - transform.position; // use this to modify the shot direction yield WaitForSeconds(delay); // wait for the flight time // then do the actual shooting: if (Physics.Raycast(transform.position, dir, hit){ // do here the usual job when something is hit: // apply damage, instantiate particles etc. } }

Comment
Add comment · Show 3 · 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 hathol · May 20, 2012 at 01:37 PM 0
Share

Will probably work in 95% of the cases. The only problem I can see is if you have an obstacle in the way that would have been passed (e.g. not hit) on a ballistic flight path while the dropped ray will falsely return a hit. But I suppose that's why you wrote "reasonably" ;)

avatar image Kaze_Senshi · May 20, 2012 at 01:49 PM 0
Share

I think that you can choose a group of layers that your raycast can "hit". Otherwise you could check if the object is passable, apply some "passing effect" and call the same raycast-bullet function in the same direction of the original-one, with the origin inside the passed object.

avatar image aldonaletto · May 20, 2012 at 03:57 PM 1
Share

@hathol, yes, that's what I mean by "reasonably": it will hit walls that could be passed in a real ballistic trajectory, and have two other possible problems: if the target moves out during the flight time, a farther object may be hit with the wrong delay and drop; and if another closer object occludes the target during flight time, again the drop and delay will be incorrect.
The rigidbody+raycast alternative you've proposed is the only real "bullet-proof" approach: you can use backward or forward raycasts each FixedUpdate, with bulletSpeed * Time.fixedDeltaTime as the distance (the distance traveled between physics cycles).

avatar image
1

Answer by hathol · May 20, 2012 at 11:58 AM

You should separate the logic from the visual representation. Use raycasting for the actual hit detection since it's more reliable. Then you can use actual bullet objects (even with rigidbody physics if you like) or any other form of visual representation of the shot (in my current project we are using a particle emitter for a machine gun for example).

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 drift501 · May 20, 2012 at 12:30 PM 0
Share

Can I simulate bullet drop and flight time with raycasting?

avatar image hathol · May 20, 2012 at 12:49 PM 2
Share

Depends on the level of accuracy you need. You can always change the angle of the ray slightly downwards or delay the casting. But if you're making a sniper simulation where you really need an accurate flight path, then no. But if you're using physics simulation and want to be sure that a bullet will not accidentaly go through an object, you could still use raycasting to double check your results. Basically fire a (very short) ray from the bullet towards it's current backward direction to make sure you didn't accidentally pass something you shouldn't have.

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

9 People are following this question.

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

Related Questions

Adding real life physics behavior on 3-d object. 1 Answer

Applying proper drag and center of mass for a vehicle 1 Answer

how to create the magnet similar to the real magnet ? 1 Answer

Calculate Distance using WheelColliders 3 Answers

Add force to Rigidbody towards clicked direction using Raycast 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