void Hit(RaycastBullet hitPoint) { Vector3 pos = hitPoint.point + hitPoint.normal * prefab.offset; GameObject hit = null; if (prefab.impact) { hit = PoolManager.Spawn(prefab.impact.gameObject, pos, Quaternion.identity); } if (!prefab.loop && hit) { PlaySound(prefab.audioHitRandom, pos, null, false); } if (prefab.force > 0 && hitPoint.rigidbody != null) { hitPoint.rigidbody.AddForceAtPosition(hitPoint.forward * prefab.force, hitPoint.point, ForceMode.VelocityChange); } if (!hitPoint.transform) { return; } Enemy enemy = hitPoint.transform.gameObject.GetComponent <Enemy>(); if (!enemy) { return; } enemy.Hit(prefab.hitpoints); }
void OnParticleCollision(GameObject other) { if (ImpactAction == null) { return; } int numCollisionEvents = ps.GetCollisionEvents(other, collisionEvents); for (int i = 0; i < numCollisionEvents; ++i) { RaycastBullet bullet = new RaycastBullet() { point = collisionEvents[i].intersection, forward = collisionEvents[i].velocity.normalized, normal = collisionEvents[i].normal, transform = other.transform, rigidbody = other.GetComponent <Rigidbody>() }; ImpactAction.Invoke(bullet); } }
// private List<ParticleCollisionEvent> // On particle collision void OnParticleCollision(GameObject other) { int numCollisionEvents = GetComponent <ParticleSystem>().GetCollisionEvents(other, collisionEvents); int i = 0; // Play collision sound and apply force to the rigidbody was hit while (i < numCollisionEvents) { if (ImpactAction != null) { RaycastBullet bullet = new RaycastBullet() { point = collisionEvents[i].intersection, forward = collisionEvents[i].velocity.normalized, normal = collisionEvents[i].normal, transform = other.transform, rigidbody = other.GetComponent <Rigidbody>() }; ImpactAction.Invoke(bullet); i++; continue; } F3DAudioController.instance.ShotGunHit(collisionEvents[i].intersection); if (other.GetComponent <Rigidbody>()) { Vector3 pos = collisionEvents[i].intersection; Vector3 force = collisionEvents[i].velocity.normalized * 50f; other.GetComponent <Rigidbody>().AddForceAtPosition(force, pos); } i++; } }
// Hit point calculation void Raycast() { // Prepare structure and create ray hitPoint = new RaycastHit(); Ray ray = new Ray(transform.position, transform.forward); // Calculate default beam proportion multiplier based on default scale and maximum length float propMult = MaxBeamLength * (beamScale / 10f); // Raycast if (Physics.Raycast(ray, out hitPoint, MaxBeamLength, layerMask)) { // Get current beam length beamLength = Vector3.Distance(transform.position, hitPoint.point); // Update line renderer if (!Oscillate) { lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength)); } // Calculate default beam proportion multiplier based on default scale and current length propMult = beamLength * (beamScale / 10f); // Adjust impact effect position if (rayImpact) { rayImpact.position = hitPoint.point - transform.forward * 0.5f; } if (ImpactAction != null) { RaycastBullet bullet = new RaycastBullet(hitPoint) { forward = transform.forward }; ImpactAction.Invoke(bullet); // Apply hit force to rigidbody } else { ApplyForce(0.1f); } } // Nothing was his else { // Set beam to maximum length beamLength = MaxBeamLength; // Update beam length if (!Oscillate) { lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength)); } // Adjust impact effect position if (rayImpact) { rayImpact.position = transform.position + transform.forward * beamLength; } } // Adjust muzzle position if (rayMuzzle) { rayMuzzle.position = transform.position + transform.forward * 0.1f; } // Set beam scaling according to its length lineRenderer.material.SetTextureScale("_MainTex", new Vector2(propMult, 1f)); }
// Hit point calculation void Raycast() { // Prepare structure and create ray //hitPoint = new RaycastHit(); Ray ray = new Ray(transform.position, transform.forward); // Calculate default beam proportion multiplier based on default scale and maximum length float propMult = MaxBeamLength * (beamScale / 10f); // Raycast if (Physics.Raycast(transform.position, transform.forward, out hitPoint, MaxBeamLength, layerMask)) { // Get current beam length and update line renderer accordingly beamLength = Vector3.Distance(transform.position, hitPoint.point); lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength)); // Calculate default beam proportion multiplier based on default scale and current length propMult = beamLength * (beamScale / 10f); // Adjust impact effect position if (rayImpact) { rayImpact.position = hitPoint.point - transform.forward * 0.5f; } if (ImpactAction != null) { RaycastBullet bullet = new RaycastBullet(hitPoint) { forward = transform.forward }; ImpactAction.Invoke(bullet); } else // Spawn prefabs and apply force //if (F3DFXController.instance) { switch (fxType) { case F3DFXType.Sniper: F3DFXController.instance.SniperImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(4f); break; case F3DFXType.RailGun: F3DFXController.instance.RailgunImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(7f); break; case F3DFXType.PlasmaBeam: ApplyForce(0.5f); break; case F3DFXType.PlasmaBeamHeavy: ApplyForce(2f); break; } } } //checking in 2d mode else { RaycastHit2D ray2D = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), new Vector2(transform.forward.x, transform.forward.y), beamLength, layerMask); if (ray2D) { // Get current beam length and update line renderer accordingly beamLength = Vector3.Distance(transform.position, ray2D.point); lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength)); // Calculate default beam proportion multiplier based on default scale and current length propMult = beamLength * (beamScale / 10f); // Spawn prefabs and apply force switch (fxType) { case F3DFXType.Sniper: F3DFXController.instance.SniperImpact(ray2D.point + ray2D.normal * fxOffset); ApplyForce(4f); break; case F3DFXType.RailGun: F3DFXController.instance.RailgunImpact(ray2D.point + ray2D.normal * fxOffset); ApplyForce(7f); break; case F3DFXType.PlasmaBeam: ApplyForce(0.5f); break; case F3DFXType.PlasmaBeamHeavy: ApplyForce(2f); break; } // Adjust impact effect position if (rayImpact) { rayImpact.position = new Vector3(ray2D.point.x, ray2D.point.y, this.gameObject.transform.position.z) - transform.forward * 0.5f; } } // Nothing was his else { // Set beam to maximum length beamLength = MaxBeamLength; lineRenderer.SetPosition(1, new Vector3(0f, 0f, beamLength)); // Adjust impact effect position if (rayImpact) { rayImpact.position = transform.position + transform.forward * beamLength; } } } // Adjust muzzle position if (rayMuzzle) { rayMuzzle.position = transform.position + transform.forward * 0.1f; } // Set beam scaling according to its length lineRenderer.material.SetTextureScale("_MainTex", new Vector2(propMult, 1f)); }
void Update() { // If something was hit if (isHit) { // Execute once if (!isFXSpawned) { isFXSpawned = true; if (ImpactAction != null) { RaycastBullet bullet = new RaycastBullet(hitPoint) { forward = transform.forward }; ImpactAction.Invoke(bullet); return; } // Invoke corresponding method that spawns FX switch (fxType) { case F3DFXType.Vulcan: F3DFXController.instance.VulcanImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(2.5f); break; case F3DFXType.SoloGun: F3DFXController.instance.SoloGunImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(25f); break; case F3DFXType.Seeker: F3DFXController.instance.SeekerImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(30f); break; case F3DFXType.PlasmaGun: F3DFXController.instance.PlasmaGunImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(25f); break; case F3DFXType.LaserImpulse: F3DFXController.instance.LaserImpulseImpact(hitPoint.point + hitPoint.normal * fxOffset); ApplyForce(25f); break; } } // Despawn current projectile if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay))) { OnProjectileDestroy(); } } // No collision occurred yet else { // Projectile step per frame based on velocity and time Vector3 step = transform.forward * Time.deltaTime * velocity; // Raycast for targets with ray length based on frame step by ray cast advance multiplier if (Physics.Raycast(transform.position, transform.forward, out hitPoint, step.magnitude * RaycastAdvance, layerMask)) { isHit = true; // Invoke delay routine if required if (DelayDespawn) { // Reset projectile timer and let particles systems stop emitting and fade out correctly timer = 0f; Delay(); } } // Nothing hit else { // Projectile despawn after run out of time if (timer >= lifeTime) { OnProjectileDestroy(); } } // Advances projectile forward transform.position += step; } // Updates projectile timer timer += Time.deltaTime; }