private void OnCollisionEnter(Collision collision) { if (checkDelay > Time.time) { return; } if (identity == null || identity.controller == null || !identity.controller.isLocalPlayer) { return; } float impulse = collision.impulse.magnitude; if (impulse > 10) { SNet_Controller.UserHit userHit = new SNet_Controller.UserHit(identity.identity); userHit.v = Mathf.RoundToInt(((collision.rigidbody != null) ? Mathf.Sqrt(collision.rigidbody.mass) : 1) * impulse * impulse / 5f); if (collision.rigidbody != null) { userHit.v = Mathf.RoundToInt(userHit.v * collision.rigidbody.mass); } userHit.h = transform.position + transform.up; SNet_Network.instance.Send_Message(userHit); } }
/// <summary> /// Some player has been hit /// </summary> /// <param name="value">Incoming message</param> private void OnUserHit(string[] value) { SNet_Controller.UserHit readMessage = SNet_Network.SNetMessage.ReadMessage <SNet_Controller.UserHit>(value[0]); readMessage.s = ulong.Parse(value[1]); SNet_Identity id = null; if ( SNet_Identity.find(readMessage.i, out id)) { /* * UI DAMAGE EFFECT * */ if (id.controller == SNet_Controller.user && !SNet_Controller.user.isDead) { UI_UserHit.instance.panel.alpha = 1; Vector3 vec1 = readMessage.h - id.transform.position; Vector3 vec2 = id.transform.forward; float angle = Vector3.Angle(vec1, vec2); Vector3 cross = Vector3.Cross(vec1, vec2); if (cross.y < 0) { angle = -angle; } UI_UserHit.instance.incoming.eulerAngles = new Vector3(0, 0, angle); } SNet_Controller.Health health = new SNet_Controller.Health(id.identity); health.v = id.controller.health.v - readMessage.v; id.controller.health = health; } }
public void Explode() { if (exploded) { return; } exploded = true; /* * EXPLOSIVE EFFECT * */ Destroy(Instantiate(explosionParticle, transform.position, explosionParticle.rotation).gameObject, 5); /* * */ if (!SNet_Network.instance.isHost()) { return; } // HITTING Collider[] colliders = Physics.OverlapSphere(transform.position, radius, ~SNet_Controller.hitMask); List <SNet_Identity> alreadyHit = new List <SNet_Identity>(); foreach (Collider c in colliders) { if (c.gameObject == gameObject) { continue; } float distance = Vector3.Distance(transform.position, c.transform.position); int tDamage = Mathf.RoundToInt(damage / (1 + Mathf.Pow(distance, 2) / 10)); if (c.gameObject.layer == 9) { // Player SNet_Identity id = c.transform.root.GetComponent <SNet_Identity>(); if (!alreadyHit.Contains(id) && !id.controller.isDead) { // If its not dead. alreadyHit.Add(id); SNet_Controller.UserHit hit = new SNet_Controller.UserHit(id.identity); hit.v = tDamage; hit.h = transform.position; SNet_Network.instance.Send_Message(hit); // Ragdoll bone push SNet_Network.instance.Send_Message(new RagdollHelper.Impact(id.identity, c.gameObject.name, c.transform.position - transform.position, tDamage / 150f)); } } else { SNet_Identity id = c.GetComponent <SNet_Identity>(); if (id != null) { if (id.rbody != null) { id.rbody.rBody.AddExplosionForce((radius - distance) * damage * id.rbody.rBody.mass / 4, transform.position, radius); id.rbody.nextUpdate = 0; // Update now. } if (id.vehicle != null) { /* * VEHICLE HEALTH IS HOST CONTROLLED * */ if (SNet_Network.instance.isHost()) { if (!alreadyHit.Contains(id)) { alreadyHit.Add(id); id.vehicle.health = new SNet_Vehicle.VH(id.identity, Mathf.RoundToInt(id.vehicle.health.v - tDamage / id.vehicle.armor)); SNet_Network.instance.Send_Message(id.vehicle.health); } } /* * */ } } if (c.gameObject.CompareTag("Explosive")) { c.gameObject.GetComponent <Explosive>().Explode_Request(); } } } Destroy(gameObject); // Destroy after explode. }