Exemplo n.º 1
0
        private void RecalculateGrindTriggers(GameObject go)
        {
            foreach (Transform transform in go.GetComponentsInChildren <Transform>())
            {
                if (transform.name.Contains("GrindSpline") && !transform.name.Contains("Colliders"))
                {
                    Transform grindColliders = go.transform.Find(transform.name + "Colliders").transform;

                    Vector3[]     grindPoints  = new Vector3[transform.childCount];
                    SplinePoint[] splinePoints = new SplinePoint[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        grindPoints[i]  = transform.GetChild(i).position;
                        splinePoints[i] = new SplinePoint(grindPoints[i]);
                    }

                    SplineComputer splineComputer = grindColliders.gameObject.GetComponent <SplineComputer>();
                    Vector3[]      grindNormals   = new Vector3[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length - 1; i++)
                    {
                        GameObject grindCollider = grindColliders.Find("RailCol" + i).gameObject;
                        grindCollider.transform.position = grindPoints[i];
                        grindCollider.transform.LookAt(grindPoints[i + 1]);
                        BoxCollider boxCollider   = grindCollider.GetComponent <BoxCollider>();
                        float       segmentLength = Vector3.Distance(grindPoints[i], grindPoints[i + 1]);
                        boxCollider.size   = new Vector3(0.08f / go.transform.lossyScale.x, 0.08f / go.transform.lossyScale.y, segmentLength);
                        boxCollider.center = Vector3.forward * segmentLength / 2f;
                        grindNormals[i]    = grindCollider.transform.up;
                    }

                    grindNormals[grindNormals.Length - 1] = grindNormals[grindNormals.Length - 2];
                    splineComputer.SetPoints(splinePoints);
                    splineComputer.Evaluate(0.9);
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        splineComputer.SetPointNormal(i, splineComputer.GetPointNormal(i, SplineComputer.Space.World) + grindNormals[i], SplineComputer.Space.World);
                    }
                }
                if (transform.name.Contains("GrindCollider"))
                {
                    transform.localScale = new Vector3(1f / go.transform.lossyScale.x, 1f / go.transform.lossyScale.y, 1f);
                }
            }
        }
        private void AddGrindTriggers(GameObject go)
        {
            foreach (Transform transform in go.GetComponentsInChildren <Transform>())
            {
                if (transform.name.Contains("GrindSpline"))
                {
                    Transform grindColliders = new GameObject(transform.name + "Colliders").transform;
                    grindColliders.parent           = go.transform;
                    grindColliders.gameObject.layer = 12;

                    if (transform.name.Contains("Metal"))
                    {
                        transform.tag = "Metal";
                    }
                    if (transform.name.Contains("Wood"))
                    {
                        transform.tag = "Wood";
                    }
                    if (transform.name.Contains("Concrete"))
                    {
                        transform.tag = "Concrete";
                    }

                    Vector3[]     grindPoints  = new Vector3[transform.childCount];
                    SplinePoint[] splinePoints = new SplinePoint[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        grindPoints[i]  = transform.GetChild(i).position;
                        splinePoints[i] = new SplinePoint(grindPoints[i]);
                    }

                    SplineComputer splineComputer = grindColliders.gameObject.AddComponent <SplineComputer>();
                    splineComputer.type = Spline.Type.Linear;
                    Vector3[] grindNormals = new Vector3[grindPoints.Length];
                    for (int i = 0; i < grindPoints.Length - 1; i++)
                    {
                        GameObject grindCollider = new GameObject("RailCol" + i);
                        grindCollider.layer = 12;
                        grindCollider.transform.position = grindPoints[i];
                        grindCollider.transform.LookAt(grindPoints[i + 1]);
                        BoxCollider boxCollider   = grindCollider.AddComponent <BoxCollider>();
                        float       segmentLength = Vector3.Distance(grindPoints[i], grindPoints[i + 1]);
                        boxCollider.size               = new Vector3(0.08f / go.transform.lossyScale.x, 0.08f / go.transform.lossyScale.y, segmentLength);
                        boxCollider.center             = Vector3.forward * segmentLength / 2f;
                        boxCollider.isTrigger          = true;
                        grindCollider.transform.parent = grindColliders;
                        grindNormals[i] = grindCollider.transform.up;

                        if (transform.name.Contains("Metal"))
                        {
                            grindCollider.tag = "Metal";
                        }
                        if (transform.name.Contains("Wood"))
                        {
                            grindCollider.tag = "Wood";
                        }
                        if (transform.name.Contains("Concrete"))
                        {
                            grindCollider.tag = "Concrete";
                        }
                    }

                    grindNormals[grindNormals.Length - 1] = grindNormals[grindNormals.Length - 2];
                    splineComputer.SetPoints(splinePoints);
                    splineComputer.Evaluate(0.9);
                    for (int i = 0; i < grindPoints.Length; i++)
                    {
                        splineComputer.SetPointNormal(i, splineComputer.GetPointNormal(i, SplineComputer.Space.World) + grindNormals[i], SplineComputer.Space.World);
                    }
                }
            }
        }