1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.Effects
5 {
6     
[RequireComponent(typeof (SphereCollider))]
7     
public class AfterburnerPhysicsForce : MonoBehaviour
8     {
9         
public float effectAngle = 15;
10         
public float effectWidth = 1;
11         
public float effectDistance = 10;
12         
public float force = 10;
13
14         
private Collider[] m_Cols;
15         
private SphereCollider m_Sphere;
16
17
18         
private void OnEnable()
19         {
20             m_Sphere = (GetComponent<Collider>()
as SphereCollider);
21         }
22
23
24         
private void FixedUpdate()
25         {
26             m_Cols = Physics.OverlapSphere(transform.position + m_Sphere.center, m_Sphere.radius);
27             
for (int n = 0; n < m_Cols.Length; ++n)
28             {
29                 
if (m_Cols[n].attachedRigidbody != null)
30                 {
31                     Vector3 localPos = transform.InverseTransformPoint(m_Cols[n].transform.position);
32                     localPos = Vector3.MoveTowards(localPos,
new Vector3(0, 0, localPos.z), effectWidth*0.5f);
33                     
float angle = Mathf.Abs(Mathf.Atan2(localPos.x, localPos.z)*Mathf.Rad2Deg);
34                     
float falloff = Mathf.InverseLerp(effectDistance, 0, localPos.magnitude);
35                     falloff *= Mathf.InverseLerp(effectAngle,
0, angle);
36                     Vector3 delta = m_Cols[n].transform.position - transform.position;
37                     m_Cols[n].attachedRigidbody.AddForceAtPosition(delta.normalized*force*falloff,
38                                                                  Vector3.Lerp(m_Cols[n].transform.position,
39                                                                               transform.TransformPoint(
0, 0, localPos.z),
40                                                                               
0.1f));
41                 }
42             }
43         }
44
45
46         
private void OnDrawGizmosSelected()
47         {
48             
//check for editor time simulation to avoid null ref
49             
if(m_Sphere == null)
50                 m_Sphere = (GetComponent<Collider>()
as SphereCollider);
51
52             m_Sphere.radius = effectDistance*.
5f;
53             m_Sphere.center =
new Vector3(0, 0, effectDistance*.5f);
54             
var directions = new Vector3[] {Vector3.up, -Vector3.up, Vector3.right, -Vector3.right};
55             
var perpDirections = new Vector3[] {-Vector3.right, Vector3.right, Vector3.up, -Vector3.up};
56             Gizmos.color =
new Color(0, 1, 0, 0.5f);
57             
for (int n = 0; n < 4; ++n)
58             {
59                 Vector3 origin = transform.position + transform.rotation*directions[n]*effectWidth*
0.5f;
60
61                 Vector3 direction =
62                     transform.TransformDirection(Quaternion.AngleAxis(effectAngle, perpDirections[n])*Vector3.forward);
63
64                 Gizmos.DrawLine(origin, origin + direction*m_Sphere.radius*
2);
65             }
66         }
67     }
68 }