• 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 Samir 1 · May 09, 2011 at 09:52 PM · raycastdamagesniper

Sniper Collateral Damage?

ok ive been working on my sniper today, and successfully having it in a beta working state(it needs more work) it dawned on me that the raycast im using doesnt carry on through the enemy so to hit another enemy.

if any of you have ever played cod, most of you will have, is there anyway such way to achieve collateral damage?

thanks :)

Comment

People who like this

0 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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Joshua · May 09, 2011 at 10:32 PM

Just use RaycastAll instead of Raycast to get info about all objects in line of the shot instead of only the first.


Edit: a working example.

I advice Raycast over collision detection of bullets anyway, because if they move fast it may not register. Raycast is a lot more reliable. I'm assuming all players are tagged player here.

var gun : Transform; //transform of the front of your gun, where you shoot from. var distance : float // how far do you want someone to be able to shoot? var damage : float; var collateral : float;

function Update () {

 if(*you shoot*) {

     target = Physics.RaycastAll(gun.position, gun.forward, distance)

     if(target[0].collider.gameObject.tag == "Player") {

         target[0].collider.gameObject.GetComponent("*healthScript*").hitPoints -= damage;

     }
     if(target[1].collider.gameObject.tag == "Player") {

         target[1].collider.gameObject.GetComponent("*healthScript*").hitPoints -= collateral;

     }

}

Now you could improve this script to for instance place a bullethole texture on the target[x].point if the target is a wall, etc.

Good luck with it!

Comment
Samir 1
superpig

People who like this

2 Show 5 · 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 Samir 1 · May 09, 2011 at 10:36 PM 0
Share

thanks i think raycastall will do the trick, ive not tried it out but i will let you know when i do :)

avatar image Joshua · May 10, 2011 at 06:33 AM 0
Share

I'll edit in an example :)

avatar image Samir 1 · May 12, 2011 at 04:18 PM 0
Share

thanks man much appreciated :)

avatar image Samir 1 · May 12, 2011 at 04:33 PM 0
Share

i get an erroe saying array index is out of range?

avatar image Joshua · May 12, 2011 at 11:40 PM 0
Share

First check if there is a first and then second collider, and then call them.

avatar image

Answer by Dave 11 · May 09, 2011 at 10:04 PM

Try to set your enemy as isTrigger, then tell it either to destroy the bullet or let the bullet go and only decrease the lifepoints of the enemy. Not sure if this works though, just trying to think with you. :)

Comment
Samir 1

People who like this

1 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
avatar image

Answer by Proggin_Barnes · May 09, 2011 at 10:27 PM

You could try an OnTriggerEnter() that makes a raycast out the back of the enemy at the same angle as the one going into him.

Comment

People who like this

0 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

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

No one has followed this question yet.

Related Questions

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 0 Answers

having problems with my raycast shooting script 0 Answers

Fall Damage Script Problem? 1 Answer

What is the Best way to use Raycast for enemy Fire? 2 Answers

Enemy damage error 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