- Home /
Making a shotgun?
I've looked at other answers and they didn't really work for me. This one works with spawning the right amount of raycast but when I try to make it random it shots in a different place than where I am currently pointing at. D: Any help?
Shooting Code:
function Shoot(){
//var direction = transform.TransformDirection(Vector3.forward);
var direction : Vector3;
var angle = 1.0;
direction = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f);
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
var trf = transform; // a little optimization
var hitRotation = transform.position;
audio.PlayOneShot(shotSound); // play the shot sound...
MuzzleFlashOn();
for (var i = 0; i < shotgunPellets; i++){ //For each pellet create a random origin and fire
var randomX : float = Random.Range(-0.01, 5.5);
var randomY : float = Random.Range(-0.01, 5.5);
hitRotation.y = randomY;
hitRotation.x = randomX;
if(Physics.Raycast (ray, hit, RayCastDist)){
//if(Physics.Raycast(ray.origin, hitRotation, hit, RayCastDist)) Not Working Code{
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.gameObject.tag == "Box"){
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", 5.0);
//Instantiate(bulletHole, hit.point, rot);
}
if(hit.collider.gameObject.tag == "Enemy"){
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
hit.collider.SendMessageUpwards("LoseHealth", 5.0);
}
if(hit.collider.gameObject.tag == "Target"){
hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
hit.rigidbody.isKinematic = false;
hit.rigidbody.useGravity = true;
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
} else {
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
ChangeWep = false;
SendMessageUpwards("Recoil");
ChangeWep = true;
}
}
ThisGun.SendMessage("Lock");
shootEnabled = false;
yield WaitForSeconds(shotInterval);
shootEnabled = true;
ThisGun.SendMessage("Unlock");
}
What happens if you set it so only two bullets come out? Does the number of bullets make a difference? If it does, it's possible that the bullets are colliding with each other.
Answer by ThermalFusion · Sep 01, 2012 at 02:05 AM
The problem lies in your generation of the random ray direction. You should pass the direction as a normalized vector, not a euler angles rotation.
var ray : Ray;
var hit : RaycastHit;
var tempVec : Vector3;
for (var i : int = 0; i < shots; i++) {
// Slerp camera forward direction towards a random direction, 0.05f is the spread, 0 means no spread, 1 means 180 degrees spread.
tempVec = Vector3.Slerp(Camera.main.transform.forward, Random.onUnitSphere, 0.05f);
ray = new Ray(Camera.main.transform.position, tempVec);
if (Physics.Raycast(ray, hit, 100f)) {
// Do your hit stuff here
GameObject.Instantiate(explosion, hit.point, Quaternion.identity);
}
}
Your answer
Follow this Question
Related Questions
not losing ammo when shooting 1 Answer
How do I make a gun project a particle? 1 Answer
FPS PISTOL 1 Answer
Raycasting to the middle of the screen? 2 Answers
How do I make a gun? 1 Answer