• Unity 汽车碰撞


    protected void OnCollisionEnter(Collision collision)
        {
            if (isCollision)
            {
                return;
            }
            int layer = collision.gameObject.layer;
            if (CheckEnableCollision(layer))
            {
                ContactPoint[] contacts = collision.contacts;
                ContactPoint contact = contacts[0];
                Vector3 point = contact.point;
                if (moveCoroutine != null)
                {
                    StopCoroutine(moveCoroutine);
                }
                animator.enabled = false;
                //Debug.Log("point==(" + point.x + "," + point.y + "," + point.z + ")" + ";
    transform.position==(" + transform.position.x + "," + transform.position.y + "," + transform.position.z + ")"
                //        + ";
    meshFilter.position==(" + meshFilter.transform.position.x + "," + meshFilter.transform.position.y + "," + meshFilter.transform.position.z + ")");
                Vector3 colliderPosition = new Vector3(point.x, colliderPos.position.y, point.z);
                GetMinDistanceFromVerticle(colliderPosition);
                Vector3 direction = (new Vector3(transform.position.x, colliderPosition.y, transform.position.z) - colliderPosition).normalized;
                float angleX = Vector3.Angle(new Vector3(direction.x, 0, direction.z), new Vector3(0, 0, 2));
                float angleZ = Vector3.Angle(new Vector3(direction.x, 0, direction.z), new Vector3(2, 0, 0));
                float rotateX = 0;
                float rotateZ = 0f;
                if (angleX > 90)
                {
                    rotateX = -15f;
                }else if(angleX < 90)
                {
                    rotateX = 15f;
                }
               if(angleZ > 90)
                {
                    rotateZ = 15;
                }else if(angleZ < 90)
                {
                    rotateZ = -15;
                }
                _rigibody.angularDrag = 0;
                _rigibody.AddForce(new Vector3(40 * direction.x, 80, 40 * direction.z),ForceMode.Impulse);
                _rigibody.angularVelocity = new Vector3(rotateX, 0, rotateZ);
                GameManager.Instance.vehicleCrashEffect.transform.position = point;
                GameManager.Instance.vehicleCrashEffect.SetActive(true);
                GameManager.Instance.vehicleCrashEffect.GetComponent<ParticleSystem>().Play();
                GameAudioManager.Instance.PlayCarContact();
                MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
                isCollision = true;
                if (!isArrivedLineEnd)
                {
                    GameEventManager.SendOnVehicleArrivedLineEndEvent(colorIdx);
                    if (this.vehicleType == VEHICLELTYPE.NORMAL)
                    {
                        GameManager.Instance.ArrivedLineEndCnt += 1;
                        isArrivedLineEnd = true;
                    }
                }
            }
        }
  • 相关阅读:
    [Leetcode Week17]Copy List with Random Pointer
    [Leetcode Week16]Range Sum Query
    [Leetcode Week16]Insertion Sort List
    [Leetcode Week15]Populating Next Right Pointers in Each Node II
    [Leetcode Week15]Populating Next Right Pointers in Each Node
    [Leetcode Week15] Add Two Numbers
    [Leetcode Week14]Path Sum II
    [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
    [Leetcode Week14]Maximum Binary Tree
    [Leetcode Week13]Search a 2D Matrix
  • 原文地址:https://www.cnblogs.com/sy-liu/p/13088175.html
Copyright © 2020-2023  润新知