• 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 MOLB · Jan 16, 2012 at 03:34 PM · raycastprojectileimpact

Bullet impact on enemy

Hi

I have a slight issue here. When i fire a bullet at the enemy, it does not react with it. But suddenly it reacts with it, but then the enemy gets thrown away by the enormous force. How can I make the bullet just colliding with it, and what is the best way to make a collider detecting function that will not let you down when needed?

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

2 Replies

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

Answer by aldonaletto · Jan 16, 2012 at 08:17 PM

Usually you should use Raycast for fast bullets, and instantiate only slow projectiles like rockets or missiles. Fast rigidbodies may "pass through" the target, because they can be before the target in one physics cycle and after it in the next. To do a raycast shot, you should do the following (weapon script):

var shotSound: AudioClip; // drag the shot sound here var impact: float = 20; // define the impact force

function Fire(){ audio.PlayOneShot(shotSound); // play the shot sound var hit: RaycastHit; // if something hit, apply the damage and the impact (only if it's a rigidbody): if (Physics.Raycast(transform.position, transform.forward, hit)){ if (hit.rigidbody){ // apply impact if the target is a rigidbody: hit.rigidbody.AddForce(transform.forward * impact); } // apply damage (ApplyDamage is an enemy function): hit.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); } } The enemy script must have a ApplyDamage to reduce its health - something like this:

var health: float = 100;

function ApplyDamage(damage: float){ health -= damage; if (health NOTE: Just to explain the big force mistery: you probably let the bullet with the default mass = 1, and perhaps the enemy too - the reaction would be like a 60Kg bullet hitting a 60Kg enemy! You should tweak the masses to have a more realistic reaction with instantiated bullets - say, 0.1 for the bullet and 10 for the enemy. This obviously isn't needed with raycast shooting, since there's no instantiated bullet - you just have to adjust the impact force.

Comment
Add comment · 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 MOLB · Jan 17, 2012 at 05:57 PM 0
Share

Thanks, but I have a problem. It says NullReferenceException. And the gun stops working for a moment. What can I do to fix this?

avatar image aldonaletto · Jan 17, 2012 at 06:19 PM 0
Share

In which line is the error? At first sight, it should happen only in two cases: 1- you've forgot to define the shotSound variable; 2- you have not added an AudioSource to the weapon (the sound is produced by audio.PlayOneShot - if there's no AudioSource added to the weapon, you may get this error)

avatar image MOLB · Jan 17, 2012 at 06:31 PM 0
Share

I know very well how to do those other things. I had those things incleded from the very start. It seems to be a problem with the Send$$anonymous$$essage line. I do not know why, because I nearly copied whan you wrote. I just made some things different.

avatar image aldonaletto · Jan 17, 2012 at 07:33 PM 0
Share

Ok, this gave me a clue: I should have written hit.transform.Send$$anonymous$$essageUpwards(...), because hit.rigidbody may not exist. I edited my answer - give it a try.

avatar image
0

Answer by SkaredCreations · Jan 16, 2012 at 03:44 PM

Set "Is Trigger" to true in the collider component so it will trigger the collision but doesn't apply physics collision (you will detect collisions using the function OnTriggerEnter through a script attached to the collider). Also, since a bullet is usually small and flies at high velocity be sure to set "Collision Detection" to "Continuous" on the bullet's RigidBody

Comment
Add comment · Show 1 · 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 MOLB · Jan 16, 2012 at 03:58 PM 0
Share

Thanks, your answer was very helpful!

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

6 People are following this question.

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

Related Questions

Need urgent help to create projectile shooting script with raycasting 1 Answer

Rotating to a direction is producing a weird result 1 Answer

Drawing a line in front of player weapon 0 Answers

Raycast Projectile w/ Physics 2 Answers

Air Strike shoot aiming at cross hairs in middle of the screen 2 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