private void OnValidate() { #if CSHARP_7_3_OR_NEWER && UNITY_2020 if (Movable == null) { Movable = GetComponent <MovablePiece>(); } if (Destructable == null) { Destructable = GetComponent <DestructablePiece>(); } #else if (_movable == null) { _movable = GetComponent <MovablePiece>(); } if (_destructable == null) { _destructable = GetComponent <DestructablePiece>(); } #endif }
void FireBullet() { GameObject muzF = (GameObject)GameObject.Instantiate(weapon.MuzzlePrefab, weapon.MuzzleTransform.position, weapon.MuzzleTransform.rotation); GameObject.Destroy(muzF, .2f); muzF.transform.SetParent(weapon.MuzzleTransform); weapon.FireRate = weapon.FireRateReset; this.transform.position -= this.transform.forward * weapon.RecoilTranslation; this.transform.Rotate(new Vector3(-1, 0, 0) * weapon.RecoilRotation); Vector3 direction = this.transform.parent.transform.forward; direction.Normalize(); myCameraAudioSource.clip = weapon.FireNoise; myAudioSource.PlayOneShot(myCameraAudioSource.clip); weapon.currentClip--; //raycasting //only raycast if we arent firing a rigidbody if (!weapon.FiresRigidbody) { //make our aesthetic-only bullet GameObject bul = (GameObject)GameObject.Instantiate(weapon.BulletPrefab, weapon.MuzzleTransform.position, weapon.MuzzleTransform.rotation); bul.GetComponent <Bullet> ().Set(weapon.MuzzleTransform.position, weapon.MuzzleTransform.position + direction, direction * 1.6f); //create the actual ray - fires from center of camera instead of gun like the bullet does Ray ray = new Ray(this.transform.parent.position, this.transform.parent.forward); RaycastHit rh; bool isHit = Physics.Raycast(ray, out rh, 100000); if (isHit) { RaycastInfo ri = rh.collider.gameObject.GetComponent <RaycastInfo>(); BodyPart bp = rh.collider.gameObject.GetComponent <BodyPart>(); if (ri != null) { Vector3 point = rh.point; Quaternion rot = Quaternion.LookRotation(rh.normal); GameObject ps = (GameObject)Instantiate(ri.onHitParticleSystem, point, rot); Vector3 newBulletDir = rh.point - weapon.MuzzleTransform.position; newBulletDir.Normalize(); bul.GetComponent <Bullet> ().Set(weapon.MuzzleTransform.position, weapon.MuzzleTransform.position + newBulletDir, newBulletDir * 1.6f); DestructablePiece dp = rh.collider.gameObject.GetComponent <DestructablePiece>(); //see if it is descturctable if (dp != null) { Destructable d = rh.collider.gameObject.transform.root.GetComponent <Destructable>(); d.Activate(400, point, 80); } } else if (bp != null) { bp.DoDamage(rh.point, Quaternion.LookRotation(rh.normal), weapon.Damage, rh.collider.gameObject.name); Vector3 newBulletDir = rh.point - weapon.MuzzleTransform.position; newBulletDir.Normalize(); bul.GetComponent <Bullet> ().Set(weapon.MuzzleTransform.position, weapon.MuzzleTransform.position + newBulletDir, newBulletDir * 1.6f); } } } else { //we are using a rocket or something. GameObject projectile = (GameObject)GameObject.Instantiate(weapon.BulletPrefab, weapon.MuzzleTransform.position, weapon.MuzzleTransform.rotation); projectile.GetComponent <Rocket> ().Init(direction, 1); //tell the "Rocket" part on the gun to become inactive, since we just shot it weapon.MuzzleTransform.gameObject.active = false; weapon.MuzzleTransform.gameObject.GetComponent <MeshRenderer>().enabled = false; //now we need to raycast to see if the user is pointing at a wall - in which case it should fire towards the wall Ray ray = new Ray(this.transform.parent.position, this.transform.parent.forward); RaycastHit rh; bool isHit = Physics.Raycast(ray, out rh, 100000); //if we have hit something, adjust the trajectory of the projectile if (isHit) { RaycastInfo ri = rh.collider.gameObject.GetComponent <RaycastInfo>(); if (ri != null) { Vector3 newBulletDir = rh.point - weapon.MuzzleTransform.position; newBulletDir.Normalize(); projectile.GetComponent <Rocket> ().Init(newBulletDir, 1); } } } //check if we should / can reload if (weapon.currentClip == 0 && weapon.ammoStockpile > 0) { StartReloading(); } else if (weapon.currentClip > 0 && weapon.BoltAction) { StartBoltAction(); } }