Esempio n. 1
0
    private void CalculateForces()
    {
        underwaterVerts = 0;

        for (var index = 0; index < GetComponent <MeshFilter>().mesh.normals.Length; index++)
        {
            worldVertPos = transform.position + transform.TransformDirection(GetComponent <MeshFilter>().mesh.vertices[index]);
            if (worldVertPos.y < waterLineHack)
            {
                // Splashes only on surface of water plane
                if (worldVertPos.y > waterLineHack - 0.1f)
                {
                    if (GetComponent <Rigidbody>().velocity.magnitude > splashVelocityThreshold || GetComponent <Rigidbody>().angularVelocity.magnitude > splashVelocityThreshold)
                    {
                        print(GetComponent <Rigidbody>().velocity.magnitude);
                        if (OnSplash != null)
                        {
                            OnSplash.Invoke(gameObject, worldVertPos, GetComponent <Rigidbody>().velocity);
                        }
                    }
                }
                Vector3 forceAmount   = (transform.TransformDirection(-GetComponent <MeshFilter>().mesh.normals[index]) * forceScalar) * Time.deltaTime;
                Vector3 forcePosition = transform.position + transform.TransformDirection(GetComponent <MeshFilter>().mesh.vertices[index]);
                GetComponent <Rigidbody>().AddForceAtPosition(forceAmount, forcePosition, ForceMode.Force);
                underwaterVerts++;
            }
            // HACK to remove sunken boats
            if (worldVertPos.y < waterLineHack - 10f)
            {
                DestroyParentGO();
                break;
            }
            // Drag for percentage underwater
            GetComponent <Rigidbody>().drag        = (underwaterVerts / (float)GetComponent <MeshFilter>().mesh.vertices.Length) * dragScalar;
            GetComponent <Rigidbody>().angularDrag = (underwaterVerts / (float)GetComponent <MeshFilter>().mesh.vertices.Length) * dragScalar;
        }
    }
Esempio n. 2
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
#if UNITY_EDITOR
        Debug.Log($"<size=23> Collided against a {collision.collider.tag}</size>");
#endif

        if (collision.collider.tag == "FireWall2")//tag comparison beware
        {
            onFireHit.Raise();
#if UNITY_EDITOR
            Debug.Log($"<size=23> You are <color=red><b> burned </b></color> into <b><color=gray>ash</color></b></size>");
#endif
            onSplash.Raise();
            this.enabled = false;
        }

        else if (Mathf.Abs(Vector2.Dot(collision.GetContact(0).normal.normalized, Vector2.left)) == 1) //This one only checks for complete perpendicular wall finicky with edges
        {
#if UNITY_EDITOR
            Debug.Log($"<size=23> You are <color=green><b> Sliding </b></color></size>");
#endif
            IsSliding = true;
            OnSlidingStart.Invoke(collision.collider);
            onSlidingStart.Raise();
        }
        else
        {
#if UNITY_EDITOR
            Debug.Log($"<size=23> You turned into a <color=purple>pulp</color> of <color=red><b> BLOOD</b></color></size>");
            //IsSliding = true;
#endif
            this.enabled = false;
            onSplash.Raise();
            OnSplash.Invoke(collision.collider);
        }
    }