Beispiel #1
0
        void FixedUpdate()
        {
            RaycastHit raycastInfo;

            // 0.1f is a small offset to start the ray from inside the character
            // it is also good to note that the transform position in the sample assets is at the base of the character

            if (PhysicsEx.CapsuleCast(
                    this.collider,
                    this.transform.position + (Vector3.up * 0.1f),
                    Vector3.down,
                    out raycastInfo,
                    this.GroundCheckDistance,
                    this.FloorLayerMask))
            {
                this.GroundNormal = raycastInfo.normal;
                this.IsGrounded   = true;
            }
            else
            {
                this.IsGrounded   = false;
                this.GroundNormal = Vector3.up;
            }

            this.DistanceToGround = raycastInfo.distance;
        }
Beispiel #2
0
    public static bool raycastExcludeRigidbody(Vector3 begin, Vector3 end, int layerMask, out RaycastHit hitInfo)
    {
        var dis = float.MaxValue;

        hitInfo = new RaycastHit();
        foreach (var hi in PhysicsEx.linecastAll(begin, end, layerMask))
        {//返回的物件s 不会按顺序 从近到远
            if (hi.rigidbody != null)
            {
                continue;
            }
            if (hi.distance < dis)
            {
                dis     = hi.distance;
                hitInfo = hi;
            }
        }
        return(dis != float.MaxValue);
    }
Beispiel #3
0
    private IEnumerator ShootLineRoutine()
    {
        while (true)
        {
            if (GameManager.Initialized == false)
            {
                yield return(null);

                continue;
            }
            //if (!Input.GetMouseButton(1))
            //{
            //    yield return null;
            //    continue;
            //}

            Vector3 mouseWorldPosition = cameraBounds.Cam.ScreenToWorldPoint(Input.mousePosition);
            Vector3 position           = new Vector3(mouseWorldPosition.x, mouseWorldPosition.y, 0);

            Vector3 normalToTarget = (position - pivot.position).normalized;

            List <Vector3> points = new List <Vector3>();

            Quaternion desiredRotation = Quaternion.LookRotation(Vector3.forward, normalToTarget);
            float      zAngle          = Vector3.SignedAngle(Vector3.up, normalToTarget, Vector3.forward);
            zAngle = Mathf.Clamp(zAngle, -60, 60);
            Quaternion clampedRotation = Quaternion.Euler(desiredRotation.eulerAngles.x, desiredRotation.eulerAngles.y, zAngle);
            pivot.rotation = clampedRotation;

            Vector3 cannonDirection     = clampedRotation * Vector3.up;
            Vector3 currentRayPosition  = shootPoint.position;
            Vector3 currentRayDirection = cannonDirection;

            points.Add(currentRayPosition);

            yield return(null);

            bool exitLoop = false;
            canShoot = false;
            while (!exitLoop)
            {
                CastResult castResult = PhysicsEx.Cast(currentRayDirection, float.MaxValue, currentRayPosition);

                if (castResult.SomethingHit)
                {
                    currentRayPosition  = castResult.ContactPoint;
                    currentRayDirection = castResult.ReflectedDirection;

                    if (castResult.FinalObjectHit)
                    {
                        exitLoop = true;
                    }

                    if (castResult.FoundCell != null)
                    {
                        CellCmp cellCmp = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData <CellCmp>(castResult.FoundCell.Entity);
                        if (cellCmp.IsEmpty)
                        {
                            canShoot = true;
                            circle.transform.position   = castResult.FoundCell.transform.position;
                            circle.transform.localScale = Vector3.one * GameManager.CellDiameter;
                        }
                    }

                    points.Add(currentRayPosition);
                }
                else
                {
                    exitLoop = true;
                }
            }

            circle.gameObject.SetActive(canShoot);

            shootLine.positionCount = points.Count;

            for (int i = 0; i < points.Count; i++)
            {
                shootLine.SetPosition(i, points[i]);

                if (i + 1 < points.Count)
                {
                    Debug.DrawLine(points[i], points[i + 1], Color.red);
                }
            }

            shootLine.startColor = shootLine.endColor = canShoot ? Color.green : Color.red;

            yield return(null);

            if (spawnBubble)
            {
                ShootingBubble spawnedBubble = Instantiate(shootingBubblePrefab, shootPoint.position, Quaternion.identity, null);
                spawnedBubble.transform.localScale = Vector3.one * GameManager.CellDiameter;
                spawnedBubble.SetNumber(nextNumber);
                spawnedBubble.SetDirection(cannonDirection);
                spawnBubble = false;

                PreloadNextNumber();
            }
        }
    }