public void TriggerEnter(GameObject caller) { CollisionInteraction interactionData = new CollisionInteraction(true /*, false*/); interactionData.SetDamage(true, _damage, _damageType, _slowDownForce, DamageDirectionType.Velocity); Destroy(gameObject); }
private void OnTriggerEnter2D(Collider2D collision) { CollisionInteraction inter = collision.gameObject.GetComponent <CollisionInteraction>(); if (inter != null) { inter.ChangeColour(Color.green); } }
private void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Plane") { // Normalize normal vector of collision Vector3 normalized = Vector3.Normalize(collision.contacts[0].normal); // Get projection vector float projection = Vector3.Dot(-v, normalized); Vector3 vp = projection * normalized; // Get changed velocity v = 2 * vp + v; SetVelocity(v - v * (resistancePP / 100.0f)); } else if (collision.gameObject.tag == "Sphere") { if (!hitOnce) { CollisionInteraction obj = collision.gameObject.GetComponent <CollisionInteraction>(); Vector3 ke1 = direction * speed * mass; Vector3 ke2 = obj.direction * obj.speed * obj.mass; Vector3 eq1 = ke1 + ke2; Vector3 eq2 = -v_elasticity * (direction * speed - obj.direction * obj.speed); Vector3 v1f = (eq1 + obj.mass * eq2) / (mass + obj.mass); Vector3 v2f = (v1f - eq2); Vector3 cp = collision.contacts[0].point; Vector3 sum = Vector3.zero; foreach (ContactPoint contact in collision.contacts) { sum += contact.point; } // Calculate Center of Contact Points if (Vector3.Distance(Vector3.zero, sum) > Mathf.Epsilon) { cp = sum / collision.contacts.Length; } // Calculate distance btw Contact point n position radius = Vector3.Distance(cp, transform.position); obj.radius = Vector3.Distance(cp, obj.transform.position); // Calculate axis of rotation axis = Vector3.Cross(direction, cp - transform.position); obj.axis = Vector3.Cross(obj.direction, cp - obj.transform.position); float angle = Vector3.Angle(direction, cp - obj.transform.position);//obj.direction); if ((angle > 178.0f && angle < 182.0f) || (angle > -2.0f && angle < 2.0f)) { obj.angleVelocity = 0.0f; } else { // F = ma, a = v / t Vector3 Newton = mass * v / Time.deltaTime; // Toruqe = F * r float torque = Vector3.Distance(Vector3.zero, Newton) * obj.radius; // Inertia = givenNumber(Decide by shape) * mass * radius * radius float I = obj.givenNumber * obj.mass * obj.radius * obj.radius; // a = torque / I float alpha = torque / I; obj.angleVelocity = alpha * Time.deltaTime; } angle = Vector3.Angle(obj.direction, cp - transform.position); if ((angle > 178.0f && angle < 182.0f) || (angle > -2.0f && angle < 2.0f)) { angleVelocity = 0.0f; } else { Vector3 Newton = obj.mass * obj.v / Time.deltaTime; float torque = Vector3.Distance(Vector3.zero, Newton) * radius; float I = givenNumber * mass * radius * radius; float alpha = torque / I; angleVelocity = alpha * Time.deltaTime; } SetVelocity(v1f - v1f * (resistancePP / 100.0f)); obj.SetVelocity(v2f - v2f * (obj.resistancePP / 100.0f)); obj.hitOnce = true; } else { hitOnce = false; } } }