x


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 :)

more ▼

asked May 09 '11 at 09:52 PM

Samir 1 gravatar image

Samir 1
147 15 15 26

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

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!

more ▼

answered May 09 '11 at 10:32 PM

Joshua gravatar image

Joshua
6.4k 19 25 70

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

May 09 '11 at 10:36 PM Samir 1

I'll edit in an example :)

May 10 '11 at 06:33 AM Joshua

thanks man much appreciated :)

May 12 '11 at 04:18 PM Samir 1

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

May 12 '11 at 04:33 PM Samir 1

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

May 12 '11 at 11:40 PM Joshua
(comments are locked)
10|3000 characters needed characters left

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. :)

more ▼

answered May 09 '11 at 10:04 PM

Dave 11 gravatar image

Dave 11
133 8 9 20

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 09 '11 at 10:27 PM

Proggin_Barnes gravatar image

Proggin_Barnes
36 4 6 13

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1528
x248
x23

asked: May 09 '11 at 09:52 PM

Seen: 661 times

Last Updated: May 09 '11 at 09:52 PM