public bool OverlapSphere(Vector3 center, float radius, out RewinderHitboxHit hit)
    {
        bool proximityHit = false;

        hit          = new RewinderHitboxHit();
        hit.Group    = Group;
        hit.Distance = -1f; // Sphere overlaps dont have a distance

        if (Group.HasProximityHitbox)
        {
            if (proximityHit = Group.ProximityHitbox.OverlapSphere(ref Matrix, center, radius))
            {
                hit.Hitbox = Group.ProximityHitbox;
            }
            else
            {
                return(false);
            }
        }

        for (int i = 0; i < Group.BodyHitboxes.Length; ++i)
        {
            RewinderHitbox hitbox = Group.BodyHitboxes[i];

            if (hitbox.OverlapSphere(ref HitboxMatrices[i], center, radius))
            {
                hit.Hitbox = hitbox;
                return(true);
            }
        }

        return(proximityHit);
    }
    public bool Raycast(Vector3 origin, Vector3 direction, out RewinderHitboxHit hit)
    {
        float distance     = 0;
        bool  proximityHit = false;

        hit          = new RewinderHitboxHit();
        hit.Group    = Group;
        hit.Distance = -1f;

        if (Group.HasProximityHitbox)
        {
            if (proximityHit = Group.ProximityHitbox.Raycast(ref Matrix, origin, direction, out distance))
            {
                hit.Hitbox   = Group.ProximityHitbox;
                hit.Distance = distance;
            }
            else
            {
                return(false);
            }
        }

        for (int i = 0; i < Group.BodyHitboxes.Length; ++i)
        {
            RewinderHitbox hitbox = Group.BodyHitboxes[i];

            if (hitbox.Raycast(ref HitboxMatrices[i], origin, direction, out distance))
            {
                hit.Hitbox   = hitbox;
                hit.Distance = distance;
                return(true);
            }
        }

        return(proximityHit);
    }
예제 #3
0
    void Update()
    {
        if (Application.isPlaying && RewinderSnapshot.Count > 0)
        {
            RewinderSnapshot snapshot = RewinderSnapshot.Find(Time.time - behind);

            if (snapshot != null)
            {
                accuracy = (float)System.Math.Round(Mathf.Abs((Time.time - behind) - snapshot.Time), 3);
                RewinderHitboxGroupSnapshot group = snapshot.Groups.First();

                // On first time, setup debug display

#if REWINDER_DEBUG
                if (debugDict.Count == 0)
                {
                    foreach (RewinderHitbox hitbox in group.Group.BodyHitboxes)
                    {
                        switch (hitbox.ColliderType)
                        {
                        case RewinderColliderType.Sphere:
                        {
                            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            go.renderer.material    = Resources.Load("RewinderDebug", typeof(Material)) as Material;
                            go.transform.localScale = hitbox.Collider.bounds.size;
                            debugDict.Add(hitbox, go);
                        }
                        break;

                        case RewinderColliderType.Box:
                        {
                            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                            go.transform.localScale = hitbox.Bounds.size;
                            go.renderer.material    = Resources.Load("RewinderDebug", typeof(Material)) as Material;
                            debugDict.Add(hitbox, go);
                        }
                        break;
                        }
                    }
                }

                // Move debug display

                for (int i = 0; i < group.HitboxMatrices_Debug.Length; ++i)
                {
                    Matrix4x4      m      = group.HitboxMatrices_Debug[i];
                    RewinderHitbox hitbox = group.Group.BodyHitboxes[i];
                    GameObject     go     = debugDict[hitbox];
                    go.transform.position = m.MultiplyPoint(Vector3.zero);
                    go.transform.rotation = group.HitboxRotations_Debug[i];
                }
#endif

                // Do collision detection (if any)

                if (Input.GetMouseButtonDown(0))
                {
                    RaycastHit rHit;
                    Ray        r = Camera.main.ScreenPointToRay(Input.mousePosition);
                    System.Diagnostics.Stopwatch sw = null;

                    switch (selected)
                    {
                    case 0:
                    {
                        List <RewinderHitboxHit> hits;

                        // Time how long the raycast takes
                        sw = System.Diagnostics.Stopwatch.StartNew();
                        RewinderSnapshot.Raycast(behind, r.origin, r.direction, out hits);
                        sw.Stop();

                        hit = hits.FirstOrDefault();
                        RewinderSnapshot.Recycle(hits);
                        sphereExample.active = false;
                    }
                    break;

                    case 1:
                        if (Physics.Raycast(r, out rHit, 1024f, 1 << 8))
                        {
                            drawPosition = rHit.point;
                            List <RewinderHitboxHit> hits;

                            // Time how long the overlap takes
                            sw = System.Diagnostics.Stopwatch.StartNew();
                            RewinderSnapshot.OverlapSphere(behind, drawPosition, 0.5f, out hits);
                            sw.Stop();

                            hit = hits.FirstOrDefault();
                            RewinderSnapshot.Recycle(hits);
                            sphereExample.transform.position = drawPosition;
                            sphereExample.active             = true;
                        }
                        break;
                    }

                    sw.Stop();
                    time = (float)((double)sw.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency);
                }
            }
        }
    }