- Home /
Shotgun script
Is there a way for me to alter a raycast gun script to make it like a shotgun? So it fires several rays that move off centre by a random, but limited amount?
Answer by Graham-Dunnett · May 24, 2011 at 12:09 PM
Yes, you can do this. Creating the rays will involve setting up a cone that points in the direction that the raycast ray points in. You'll need a couple of vectors A and B that are perpendicular to the raycast direction (use a cross product to get these.) Now move along your raycast ray some distance l, and pick a random number a and b in the range +/-R, and compute q=a*a+b*b. If q > R*R then try another pair of a and b. Generate a vector aA + bB and add it to where you are along the raycast ray. a and b choose points that are inside a circle of radius R. Adjust R and l to suit how widespread your shotgun shoots pellets. Now you know how to create the shotgun raycast rays just use them to do collision detection. Really can't advise you how many of these rays you'll need, am sure a real shot gun has hundreds if not thousands of pellets.
I don't really understand this. I'm sure everything works fine, but I am new to raycasting. Where would I start?
Answer by KnC_Studios · Jul 29, 2013 at 06:19 PM
I used this. I tweaked it from the Fps Tutorial MachineGun script. All I added was a varying degree of inaccuracy, and made it shoot all its raycasts at once. Hope this helps! var range = 70.0; var playerWeapons : GameObject; var fireRate = 0.0005; var force = 50.0; var damage = 6.0; var bulletsPerClip = 20; var clips = 20; var reloadTime = 2; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer; public var spreadFactor : float = 0.1; var reload : AudioClip; private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
function Start () {
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() {
if (muzzleFlash) {
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () {
if (bulletsLeft == 0)
return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
//var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var direction : Vector3 = transform.forward;
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// 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();
}
function Reload () {
playerWeapons.GetComponent(PlayerWeapons).enabled = false;
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
playerWeapons.GetComponent(PlayerWeapons).enabled = true;
// We have a clip left reload
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
audio.PlayOneShot(reload, 1.0 / audio.volume);
}
}
function GetBulletsLeft () {
return bulletsLeft;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612083926im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with Shotgun 1 Answer
Shotgun raycast 1 Answer
Making a shotgun 3 Answers
Raycast Shotgun 1 Answer
Shotgun Spread Issue 1 Answer