- Home /
MachineGun script applied damage to the object that is not hit.
Here is my script that I have modified from the FPS Tutorial. I have made it so that it has a random spread rather than dead on accurate fire.
function FireOneShot (shotOwner : boolean, hitPoint : Vector3, hitNormal : Vector3) { var direction = SprayDirection(); var hit : RaycastHit; print("shot"); // Did we hit anything? //Invoke("muzzleClimb", 0.3); if(shotSpread <= spreadMax) shotSpread += spreadClimbAmount; if (Physics.Raycast (transform.position, direction, hit, range)) { var settingsArray = new String[2]; settingsArray[0]=damage+""; settingsArray[1]=localPlayerName;
hit.collider.SendMessage("ApplyDamage", settingsArray, SendMessageOptions.DontRequireReceiver);
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
var go : GameObject;
var delta : float = -0.02;
var hitUpDir : Vector3 = hit.normal;
hitPoint = hit.point + hit.normal * delta;
//what type of object did we hit?
switch(hit.collider.gameObject.tag)
{
case "wood":
go = Network.Instantiate(woodParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "metal":
go = Network.Instantiate(metalParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "concrete":
go = Network.Instantiate(concreteParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "dirt":
go = Network.Instantiate(sandParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "explosive":
var expObject : explosiveObject = hit.collider.gameObject.GetComponent(explosiveObject);
expObject.hitTimeCount +=1;
go = Network.Instantiate(metalParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "sand":
go = Network.Instantiate(sandParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "water":
go = Network.Instantiate(waterParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
case "acid":
go = Network.Instantiate(acidParticle, hit.point, Quaternion.FromToRotation(Vector3.up, hitUpDir), 0);
break;
default:
return;
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
and here is the function for making the direction random. l
function SprayDirection() {
var vx = (1 - 2 * Random.value) * shotSpread;
var vy = (1 - 2 * Random.value) * shotSpread;
var vz = 1.0;
return transform.TransformDirection(Vector3(vx,vy,vz));
}
The problem I am having is that on this line
hit.collider.SendMessage("ApplyDamage", settingsArray, SendMessageOptions.DontRequireReceiver);
It still applies the damage to the item in the immediate forward direction rather than the random area. help is appreciated :)
What is not working about this script is lets say I looked straight on to another player and I shot. You could see sparks instantiated in the correct random area, but instead of damage being applied to that object, it was applied to the player because he was in the immediate forward direction of me.
Sorry I can't help with your question I'm new to code so this stuff baffles me buggy sometimes. But this could be EXACTLY what I need as I'm trying to do the same thing for my FPS game. But where do you put your particle systems for a setup like this? Are they all children of your individual weapons?
Add me on AI$$anonymous$$ and I can help you lol. BTW this isnt the entire script, so certain things might not work
I added you I think, my username is $$anonymous$$ichael $$anonymous$$night As long as I did that correctly, I've never had an AI$$anonymous$$ account before ;)
Depending on the spread you'd still expect some bullets to hit directly ahead right? Isn't that kind of the point?
Answer by Fierce Waffle · Mar 27, 2011 at 11:35 AM
Solved. I dont know how I fixed it lol.
Your answer

Follow this Question
Related Questions
Raycast Decals,Sound and MuzzleFlash not working too well 0 Answers
How to restrict Direction? 0 Answers
Why i can't see my questions ? 2 Answers
Skyboxes, GetPixel and Raycast Directions 1 Answer
RayCast to all directions 4 Answers