- Home /
Question by
Weirdo-Studios · Jul 10, 2013 at 04:43 PM ·
raycastfpsbulletsdecalmuzzleflash
Raycast Decals,Sound and MuzzleFlash not working too well
Ok so i have all my code but the decals dont show up and their is no sound or flash on shooting apart from when i don't hit anything (my level has no ceiling so i shoot upwards) the reload works fine but when i shoot at a wall not my crates (they have rigid bodies) then the timer for shooting gets ignored too... I really don't know why this wont work i have spent ages trying to get it working.
MY SHOOT SCRIPT (Pretty long yes, i know, awkward).
var Damage : int = 20;
var Range : float = 1000;
var Force : float = 500;
var Clips : int = 2;
var BulletsPerClip : int = 12;
var ReloadTime : float = 2;
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.6;
public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;
var Aiming: boolean = false;
var Gun : GameObject;
var HitParticles : ParticleEmitter;
var MuzzleFlash : ParticleEmitter;
var MuzzleCooler : float = 0.6;
var MuzzleTimer : float;
var Light1 : GameObject;
var Light2 : GameObject;
var Light3 : GameObject;
var MuzzleSpeed : int = 200000;
var Decals : GameObject;
var DecalDis : float = 0.01;
var hit = RaycastHit;
function Start(){
MuzzleFlash.emit = false;
HitParticles.emit = false;
BulletsLeft = BulletsPerClip;
Gun.animation["Reload"].layer = 1;
}
function Update () {
if (MuzzleTimer > 0){
MuzzleTimer -= Time.deltaTime;
MuzzleFlashShow();
}
if(MuzzleTimer < 0){
MuzzleTimer = 0;
}
if (ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if(ShootTimer < 0){
ShootTimer = 0;
}
if(Input.GetMouseButtonDown(0) && BulletsLeft)
{
if(ShootTimer ==0){
RayShoot();
PlayShootAudio();
ShootTimer = ShootCooler;
}
if(MuzzleTimer ==0){
MuzzleTimer = MuzzleCooler;
MuzzleFlashShow();
}
}
if (Input.GetMouseButtonUp(1))
{
Aiming = false;
}
if (Input.GetMouseButtonDown(1))
{
Aiming = true;
}
}
function RayShoot () {
//check aim
if(Aiming == true){
Gun.animation.Play("ShootingAiming");
}
//check aim
else if(Aiming == false){
Gun.animation.Play("ShootingIdle");
}
//take bullet
BulletsLeft --;
//check bulletleft
if(BulletsLeft <0){
BulletsLeft = 0;
}
//check bulletleft
if(BulletsLeft == 0){
Reload();
}
var Hit : RaycastHit;
var DirectionRay : Vector3 = transform.TransformDirection(Vector3.forward);
//debug
Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
//if contact
if(Physics.Raycast(transform.position , DirectionRay , Hit , Range)){
//if it's rigidbody
if(Hit.rigidbody) {
//particle effects
HitParticles.transform.position = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit();
//force and dmg
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
Hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
if(Hit.collider){
//spawn decal
Instantiate(Decals, Hit.point +(Hit.normal * DecalDis),hit.Rotation);
//particle effects
HitParticles.transform.position = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit();
}
}
}
function Reload() {
PlayReloadAudio();
Gun.animation.CrossFade("Reload", 0.4);
yield WaitForSeconds(ReloadTime);
if(Clips > 0){
BulletsLeft = BulletsPerClip;
}
}
function PlayShootAudio(){
audio.PlayOneShot(ShootAudio);
}
function PlayReloadAudio(){
audio.PlayOneShot(ReloadAudio);
}
function MuzzleFlashShow(){
if(MuzzleTimer > 0){
MuzzleFlash.emit = false;
Light1.active = false;
Light2.active = false;
Light3.active = false;
}
if(MuzzleTimer == MuzzleCooler){
MuzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * MuzzleSpeed, Vector3.forward);
MuzzleFlash.emit = true;
Light1.active = true;
Light2.active = true;
Light3.active = true;
}
}
Comment