| . | |
| using System; |
| |
| using UnityEngine; |
| |
| |
| |
| public class FloatingTerrain : MonoBehaviour |
| |
| { |
| |
| private void Start() |
| |
| { |
| |
| this.m_body = base.GetComponent<Rigidbody>(); |
| |
| this.m_collider = base.GetComponentInChildren<BoxCollider>(); |
| |
| base.InvokeRepeating("UpdateTerrain", UnityEngine.Random.Range(0.1f, 0.4f), 0.24f); |
| |
| this.UpdateTerrain(); |
| |
| } |
| |
| |
| |
| private void UpdateTerrain() |
| |
| { |
| |
| if (!this.m_lastHeightmap) |
| |
| { |
| |
| this.m_targetOffset = 0f; |
| |
| return; |
| |
| } |
| |
| this.m_targetOffset = this.m_lastHeightmap.GetHeightOffset(base.transform.position) + this.m_padding; |
| |
| if (!this.m_dummy) |
| |
| { |
| |
| GameObject gameObject = new GameObject(); |
| |
| if (this.m_copyLayer) |
| |
| { |
| |
| gameObject.layer = base.gameObject.layer; |
| |
| } |
| |
| this.m_dummy = gameObject.AddComponent<FloatingTerrainDummy>(); |
| |
| this.m_dummy.m_parent = this; |
| |
| this.m_dummyBody = gameObject.AddComponent<Rigidbody>(); |
| |
| this.m_dummyBody.mass = this.m_body.mass; |
| |
| this.m_dummyBody.drag = this.m_body.drag; |
| |
| this.m_dummyBody.angularDrag = this.m_body.angularDrag; |
| |
| this.m_dummyBody.constraints = this.m_body.constraints; |
| |
| this.m_dummyCollider = gameObject.AddComponent<BoxCollider>(); |
| |
| this.m_dummyCollider.center = this.m_collider.center; |
| |
| this.m_dummyCollider.size = this.m_collider.size; |
| |
| if (this.m_collider.gameObject != this) |
| |
| { |
| |
| this.m_dummyCollider.size = Vector3.Scale(this.m_collider.size, this.m_collider.transform.localScale); |
| |
| this.m_dummyCollider.center = Vector3.Scale(this.m_collider.center, this.m_collider.transform.localScale); |
| |
| this.m_dummyCollider.center -= this.m_collider.transform.localPosition; |
| |
| } |
| |
| gameObject.transform.parent = base.transform.parent; |
| |
| gameObject.transform.position = base.transform.position; |
| |
| this.m_collider.isTrigger = true; |
| |
| UnityEngine.Object.Destroy(this.m_body); |
| |
| } |
| |
| } |
| |
| |
| |
| private void FixedUpdate() |
| |
| { |
| |
| if (this.m_dummy) |
| |
| { |
| |
| float maxCorrectionSpeed = this.m_maxCorrectionSpeed; |
| |
| float num = this.m_targetOffset - this.m_currentOffset; |
| |
| this.m_currentOffset += Mathf.Clamp(num, -maxCorrectionSpeed, maxCorrectionSpeed); |
| |
| float num2 = this.m_currentOffset; |
| |
| if (this.m_waveFreq > 0f && num2 > this.m_waveMinOffset) |
| |
| { |
| |
| this.m_waveTime += Time.fixedDeltaTime; |
| |
| num2 += Mathf.Cos(this.m_waveTime * this.m_waveFreq) * this.m_waveAmp; |
| |
| } |
| |
| base.transform.position = this.m_dummy.transform.position + new Vector3(0f, num2, 0f); |
| |
| base.transform.rotation = this.m_dummy.transform.rotation; |
| |
| } |
| |
| } |
| |
| |
| |
| public void OnDummyCollision(Collision collision) |
| |
| { |
| |
| this.OnCollisionStay(collision); |
| |
| } |
| |
| |
| |
| private void OnCollisionStay(Collision collision) |
| |
| { |
| |
| Heightmap component = collision.gameObject.GetComponent<Heightmap>(); |
| |
| if (component != null) |
| |
| { |
| |
| this.m_lastGroundNormal = collision.contacts[0].normal; |
| |
| this.m_lastHeightmapTime = Time.time; |
| |
| if (this.m_lastHeightmap != component) |
| |
| { |
| |
| this.m_lastHeightmap = component; |
| |
| this.UpdateTerrain(); |
| |
| return; |
| |
| } |
| |
| } |
| |
| else if (this.m_lastHeightmapTime > 0.2f) |
| |
| { |
| |
| this.m_lastHeightmap = null; |
| |
| } |
| |
| } |
| |
| |
| |
| private void OnDrawGizmos() |
| |
| { |
| |
| if (this.m_dummyCollider && this.m_dummyCollider.enabled) |
| |
| { |
| |
| Gizmos.color = Color.yellow; |
| |
| Gizmos.matrix = Matrix4x4.TRS(this.m_dummyCollider.transform.position, this.m_dummyCollider.transform.rotation, this.m_dummyCollider.transform.lossyScale); |
| |
| Gizmos.DrawWireCube(this.m_dummyCollider.center, this.m_dummyCollider.size); |
| |
| } |
| |
| if (this.m_dummy != null) |
| |
| { |
| |
| Gizmos.DrawLine(base.transform.position, base.transform.position + new Vector3(0f, this.m_currentOffset, 0f)); |
| |
| } |
| |
| } |
| |
| |
| |
| private void OnDestroy() |
| |
| { |
| |
| if (this.m_dummy) |
| |
| { |
| |
| UnityEngine.Object.Destroy(this.m_dummy.gameObject); |
| |
| } |
| |
| } |
| |
| |
| |
| public static Rigidbody GetBody(GameObject obj) |
| |
| { |
| |
| FloatingTerrain component = obj.GetComponent<FloatingTerrain>(); |
| |
| if (component != null && component.m_dummy && component.m_dummyBody) |
| |
| { |
| |
| return component.m_dummyBody; |
| |
| } |
| |
| return null; |
| |
| } |
| |
| |
| |
| public float m_padding; |
| |
| |
| |
| public float m_waveMinOffset; |
| |
| |
| |
| public float m_waveFreq; |
| |
| |
| |
| public float m_waveAmp; |
| |
| |
| |
| public FloatingTerrainDummy m_dummy; |
| |
| |
| |
| public float m_maxCorrectionSpeed = 0.025f; |
| |
| |
| |
| public bool m_copyLayer = true; |
| |
| |
| |
| private Rigidbody m_body; |
| |
| |
| |
| [NonSerialized] |
| |
| public Rigidbody m_dummyBody; |
| |
| |
| |
| private BoxCollider m_collider; |
| |
| |
| |
| private BoxCollider m_dummyCollider; |
| |
| |
| |
| private Heightmap m_lastHeightmap; |
| |
| |
| |
| private Vector3 m_lastGroundNormal; |
| |
| |
| |
| private float m_targetOffset; |
| |
| |
| |
| private float m_currentOffset; |
| |
| |
| |
| private float m_lastHeightmapTime; |
| |
| |
| |
| private float m_waveTime; |
| |
| } |
| |
| |