コード例 #1
0
ファイル: Car.cs プロジェクト: nizsanli/trax
    /*
     * void OnCollisionEnter(Collision col)
     * {
     *      float breakAmt = 5f;
     *      Rigidbody rig;
     *
     *      foreach (ContactPoint contact in col.contacts)
     *      {
     *              if (Vector3.Dot(contact.normal, col.relativeVelocity) > breakAmt)
     *              {
     *                      if (contact.thisCollider.gameObject.name == "fl")
     *                      {
     *                              //Destroy(frontLeft.gameObject);
     *                              //contact.thisCollider.transform.parent.parent = null;
     *                              frontLeft = null;
     *                              attachedFL = false;
     *                              //rig = wheelFL.gameObject.AddComponent<Rigidbody>();
     *                      }
     *                      else if (contact.thisCollider.gameObject.name == "fr")
     *                      {
     *                              //Destroy(frontRight.gameObject);
     *                              //contact.thisCollider.transform.parent.parent = null;
     *                              frontRight = null;
     *                              attachedFR = false;
     *                              //rig = wheelFR.gameObject.AddComponent<Rigidbody>();
     *                      }
     *                      else if (contact.thisCollider.gameObject.name == "bl")
     *                      {
     *                              //Destroy(backLeft.gameObject);
     *                              //contact.thisCollider.transform.parent.parent = null;
     *                              backLeft = null;
     *                              attachedBL = false;
     *                              //rig = wheelBL.gameObject.AddComponent<Rigidbody>();
     *                      }
     *                      else if (contact.thisCollider.gameObject.name == "bl")
     *                      {
     *                              //Destroy(backRight.gameObject);
     *                              //contact.thisCollider.transform.parent.parent = null;
     *                              backRight = null;
     *                              attachedBR = false;
     *                              //rig = wheelBR.gameObject.AddComponent<Rigidbody>();
     *                      }
     *              }
     *      }
     * }
     */

    void CheckDamage()
    {
        Rigidbody rig;

        float dmgAngle = 45f;
        float dmgSpeed = 15f;

        RaycastHit hitInfo;

        if (attachedFL && frontLeft.Raycast(new Ray(wheelFL.position, GetComponent <Rigidbody>().velocity.normalized), out hitInfo, GetComponent <Rigidbody>().velocity.magnitude))
        {
            Destroy(frontLeft);
            frontLeft  = null;
            attachedFL = false;
            rig        = wheelFL.gameObject.AddComponent <Rigidbody>();
        }
        if (attachedFR && frontRight.Raycast(new Ray(wheelFR.position, GetComponent <Rigidbody>().velocity.normalized), out hitInfo, GetComponent <Rigidbody>().velocity.magnitude))
        {
            Destroy(frontRight);
            frontRight = null;
            attachedFR = false;
            rig        = wheelFR.gameObject.AddComponent <Rigidbody>();
        }
        if (attachedBL && backLeft.Raycast(new Ray(wheelBL.position, GetComponent <Rigidbody>().velocity.normalized), out hitInfo, GetComponent <Rigidbody>().velocity.magnitude))
        {
            Destroy(backLeft);
            backLeft   = null;
            attachedBL = false;
            rig        = wheelBL.gameObject.AddComponent <Rigidbody>();
        }
        if (attachedBR && backRight.Raycast(new Ray(wheelBR.position, GetComponent <Rigidbody>().velocity.normalized), out hitInfo, GetComponent <Rigidbody>().velocity.magnitude))
        {
            Destroy(backRight);
            backRight  = null;
            attachedBR = false;
            rig        = wheelBR.gameObject.AddComponent <Rigidbody>();
        }

        bool wheelUpDmg = false;

        if (Mathf.Acos(Vector3.Dot(GetComponent <Rigidbody>().velocity.normalized, -transform.up)) * Mathf.Rad2Deg < dmgAngle &&
            GetComponent <Rigidbody>().velocity.magnitude *2.16f > dmgSpeed)
        {
            // break wheel if grounded
            if (attachedFL && frontLeft.isGrounded)
            {
                Destroy(frontLeft);
                frontLeft  = null;
                attachedFL = false;
                rig        = wheelFL.gameObject.AddComponent <Rigidbody>();
            }
            if (attachedFR && frontRight.isGrounded)
            {
                Destroy(frontRight);
                frontRight = null;
                attachedFR = false;
                rig        = wheelFR.gameObject.AddComponent <Rigidbody>();
            }
            if (attachedBL && backLeft.isGrounded)
            {
                Destroy(backLeft);
                backLeft   = null;
                attachedBL = false;
                rig        = wheelBL.gameObject.AddComponent <Rigidbody>();
            }
            if (attachedBR && backRight.isGrounded)
            {
                Destroy(backRight);
                backRight  = null;
                attachedBR = false;
                rig        = wheelBR.gameObject.AddComponent <Rigidbody>();
            }
        }
    }