Exemple #1
0
    public Vector3 RandomPointInAnnulus(Vector2 origin, float minRadius, float maxRadius)
    {
        Vector2 point;
        float   angleArea;
        Vector3 V1;
        Vector3 V2;
        Vector3 newPosDirection;
        Vector3 newPos;

        Vector3 vec;
        Vector3 distance;
        float   v;

        do
        {
            var randomDirection = (Random.insideUnitCircle * origin).normalized;
            var randomDistance  = Random.Range(minRadius, maxRadius);
            point  = origin + randomDirection * randomDistance;
            newPos = new Vector3(point.x, 0f, point.y);

            newPosDirection = newPos - enemy.position;

            distance = enemy.transform.position - transform.position;

            v = Vector3.SignedAngle(distance, newPosDirection, Vector3.up);
        }while ((Vector3.SignedAngle(distance, newPosDirection, Vector3.up) < 45 && Vector3.SignedAngle(distance, newPosDirection, Vector3.up) > -45) ||
                (Vector3.SignedAngle(distance, newPosDirection, Vector3.up) > 135 || Vector3.SignedAngle(distance, newPosDirection, Vector3.up) < -135));   // (Vector3.Angle(V1, newPosDirection) < angleArea && Vector3.Angle(V2, newPosDirection) < angleArea));

        v = Vector3.SignedAngle(distance, newPosDirection, Vector3.up);
        //Debug.DrawRay(new Vector3(0, 0, 0), newPosDirection * 50, Color.magenta, 5);
        //Debug.DrawRay(new Vector3(0,0,0), distance * 10, Color.cyan, 5);
        return(newPos);
    }
    protected override IEnumerator Shoot()
    {
        Minions[] minionsInRange = {};
        // TODO: Add condition (with round end)
        while (true)
        {
            yield return(new WaitUntil(() =>
            {
                minionsInRange = MinionsInRange();
                return minionsInRange.Length != 0;
            }));

            var shooterTransform = Shooter;
            var shooterPos       = shooterTransform.position;
            if (minionsInRange[0] != null)
            {
                var targetDir = shooterPos - minionsInRange[0].transform.position;
                var angle     = Vector3.SignedAngle(shooterTransform.forward, targetDir, Vector3.up);
                shooterTransform.RotateAround(shooterPos, Vector3.up, angle); // Make archer look at enemy
            }

            Weapon.Create(CurrLevel.bullet, minionsInRange[0], shooterTransform, CurrLevel.damage);
            AudioSource.PlayClipAtPoint(shootingSound, transform.position);

            yield return(new WaitForSeconds(CurrLevel.secPerShot));
        }
    }
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Pit"))
        {
            if (!dodgingPit)
            {
                dodgingPit = true;
                pitDir     = currentDir;

                ContactPoint2D[] contacts = new ContactPoint2D[1];
                collision.GetContacts(contacts);
                Vector3 toCollision = new Vector3(contacts[0].point.x, contacts[0].point.y, 0) - this.transform.position;
                Vector3 toPitCenter = collision.gameObject.transform.position - this.transform.position;
                float   angle       = Vector3.SignedAngle(toPitCenter, toCollision, Vector3.forward);

                if (angle < 0)
                {
                    dodgingLeft = true;
                }
                else
                {
                    dodgingLeft = false;
                }
            }

            dodgeCounter += 2;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        switch (collision.tag)
        {
        case "Bullet":
            collision.gameObject.GetComponent <Bullet>().onHit(false);
            die();
            break;

        case "PiercingBullet":
            collision.gameObject.GetComponent <PiercingBullet>().onHit(false);
            die();
            break;

        case "ExplosiveBullet":
            collision.gameObject.GetComponent <ExplosiveBullet>().onHit();
            break;

        case "EnemyBullet":
        case "Powerup":
            break;

        case "Pit":
            dodgingPit = true;

            ContactPoint2D[] contacts = new ContactPoint2D[1];
            collision.GetContacts(contacts);
            Vector3 toCollision = new Vector3(contacts[0].point.x, contacts[0].point.y, 0) - this.transform.position;
            Vector3 toPitCenter = collision.gameObject.transform.position - this.transform.position;
            float   angle       = Vector3.SignedAngle(toPitCenter, toCollision, Vector3.forward);
            if (angle < 0)
            {
                dodgingLeft = true;
            }
            else
            {
                dodgingLeft = false;
            }

            dodgeCounter = 20;
            pitDir       = currentDir;
            break;

        default:
            Debug.Log("Unrecognized tag in OnTriggerEnter2D in Enemy! Tag: " + collision.tag);
            break;
        }
    }
    private void FixedUpdate()
    {
        //Set out Ai agents positio so we always have an up to date path
        SetAgentPosition();

        //Update the path every physics update
        latestPath = GeneratePath(target.transform.position);

        if (latestPath.corners == null || latestPath.corners.Length == 0)
        {
            return;
        }

        //Get the next position that we can get to in the path
        nextPos = GetNextPosInPath(latestPath);

        //If the car is stuck then set it in reverse for a
        //length of time to try and get out
        if (IsCarStuck())
        {
            if (currentGear != Gear.Reverse)
            {
                carStuckTimer += Time.deltaTime;
                if (carStuckTimer > carStuckTimeLimit)
                {
                    StartCoroutine(SetGearForTime(Gear.Reverse, carReverseTime));
                    carStuckTimer = 0f;
                }
            }
        }


        //Get the angle to the next position
        float angleToNextPos = Vector3.SignedAngle(transform.forward, nextPos - transform.position, transform.up);
        //Get the acceleration that we need
        float accelerationInput = normalAcceleration * (int)currentGear;

        //Relay Inputs to Car
        controllingCar.CarSteerAngle      = angleToNextPos * (int)currentGear;
        controllingCar.CarAccelerateInput = accelerationInput;
    }
Exemple #6
0
        public void Decompose(out Vector2 translation, out float rotation, out Vector2 scale)
        {
            var normal = new float3(0, 0, 1);

            var u = (Vector3)U;
            var v = (Vector3)V;

            if (Math.Abs(u.x) < kSnapEpsilon)
            {
                u.x = 0;
            }
            if (Math.Abs(u.y) < kSnapEpsilon)
            {
                u.y = 0;
            }
            if (Math.Abs(u.z) < kSnapEpsilon)
            {
                u.z = 0;
            }

            if (Math.Abs(v.x) < kSnapEpsilon)
            {
                v.x = 0;
            }
            if (Math.Abs(v.y) < kSnapEpsilon)
            {
                v.y = 0;
            }
            if (Math.Abs(v.z) < kSnapEpsilon)
            {
                v.z = 0;
            }

            //var inverted = Vector3.Cross(U, V).z < 0.0f;

            var rotationU = -Vector3.SignedAngle(Vector3.right, u, normal);
            var rotationV = -Vector3.SignedAngle(Vector3.up, v, normal);


            rotation = Math.Abs(rotationU) < Math.Abs(rotationV) ? rotationU : rotationV;

            const double min_rotate = 1.0 / 10000.0;

            rotation = (float)(Math.Round(rotation / min_rotate) * min_rotate);

            var rotation2d = Quaternion.AngleAxis(rotation, normal);
            var invRotate  = Quaternion.Inverse(rotation2d);

            u = (invRotate * U).normalized;
            v = (invRotate * V).normalized;

            var udir = Vector3.right;
            var vdir = Vector3.up;

            var uScale = Vector3.Dot(udir, u);
            var vScale = Vector3.Dot(vdir, v);

            scale = new Vector2(uScale, vScale);

            scale.x = (float)(Math.Round(scale.x / kScaleStep) * kScaleStep);
            scale.y = (float)(Math.Round(scale.y / kScaleStep) * kScaleStep);

            scale.x = (float)Math.Max(Math.Abs(scale.x), kMinScale) * Math.Sign(scale.x);
            scale.y = (float)Math.Max(Math.Abs(scale.y), kMinScale) * Math.Sign(scale.y);


            //var rotation2d  = Quaternion.AngleAxis(-rotation, Vector3.forward);
            translation = new Vector2(U.w, V.w);

            const double min_translation = 1.0 / 32768.0;

            translation.x = (float)(Math.Round(translation.x / min_translation) * min_translation);
            translation.y = (float)(Math.Round(translation.y / min_translation) * min_translation);


            // TODO: figure out a better way to find if we scale negatively
            var newUvMatrix = UVMatrix.TRS(translation, rotation, scale);

            if (Vector3.Dot((Vector3)V, (Vector3)newUvMatrix.V) < 0)
            {
                scale.y = -scale.y;
            }
            if (Vector3.Dot((Vector3)U, (Vector3)newUvMatrix.U) < 0)
            {
                scale.x = -scale.x;
            }
        }