- Home /
How do I use raycast to simulate gun fire?
Currently, I have a 3d model of a gun that I imported from blender (150 polygons) and I want to use raycast to simulate firing.
I have no experience with raycasts so any help is welcome.
If possible I would like to know how to make raycasts do damage to my sombies (they have a collider head BTW)
First, you get your raycasts. Then you coat them in steel, and set them on fire, and they will deal damage do your xombies! You need to hurl them pretty hard, and make sure you don't burn your hands when you're firing them.
Answer by ninjarob · Oct 30, 2011 at 08:00 PM
try this
put this code on your gun this will fire along the Z axis, blue arrow in the editor from the guns transform position
var damageDone:float = 1;
function FireGun(){
var hit:RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit , Mathf.Infinity)) {
hit.collider.gameObject.SendMessage("BeenShot",damageDone,SendMessageOptions.DontRequireReceiver);
}
}
Put this on your zombies (the game object with the collider)
function BeenShot(Damage:float){
//code for been shot
}
edit: added SendMessageOptions.DontRequireReceiver to send message so you dont get an error when you shoot objects other than the targets head
Your answer
![](https://koobas.hobune.stream/wayback/20220613024609im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI display weapon name you are looking at 1 Answer
Best way for a shotgun fire? 1 Answer
Raycasting Weapons with two cameras? 1 Answer
Pickup Weapons with raycast? JS 1 Answer
Errors from raycast in C# 1 Answer