squareEuclidianDistance() public static method

Euclidian distance without square root
public static squareEuclidianDistance ( Vector3 pos1, Vector3 pos2 ) : float
pos1 Vector3
pos2 Vector3
return float
    // Update is called once per frame
    void Update()
    {
        if (this.readyToFire && this.inPositionToFire)
        {
            float distanceToPlayer = DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position);

            GameObject clone = (GameObject)Instantiate(this.projectile, this.attackSpawnPoint.position, this.attackSpawnPoint.rotation);

            clone.GetComponent <Rigidbody>().AddForce(clone.transform.forward * DistanceCalculator.squareEuclidianDistance(attackSpawnPoint.position, Player.Instance.transform.position));

            /*
             * if (distanceToPlayer >= Mathf.Pow(calculationSwitchDistance, 2f))
             * {
             *                  clone.GetComponent<Rigidbody>().AddForce(clone.transform.forward * Vector3.Distance(attackSpawnPoint.position, Player.Instance.transform.position) * 10);
             * }
             * else
             * {
             *
             *  float shellVelocityX = (Vector3.Distance(Player.Instance.transform.position, attackSpawnPoint.position) / timeToTarget);
             *  float shellVelocityY = ((maxShellHeight - 4.9f * Mathf.Pow(timeToTarget, 2)) / timeToTarget);
             *  float shellVelocity = Mathf.Sqrt(Mathf.Pow(shellVelocityX, 2) + Mathf.Pow(shellVelocityY, 2)) / 2f;
             *
             *  clone.GetComponent<Rigidbody>().AddForce(clone.transform.forward * shellVelocity, ForceMode.Impulse);
             * }*/

            if (this.bossAttack)
            {
                if (this.delayedFire)
                {
                    bossAnimator.SetTrigger("FireLeftArtillery");
                }
                else
                {
                    bossAnimator.SetTrigger("FireRightArtillery");
                }
            }

            this.readyToFire      = false;
            this.inPositionToFire = false;
            StartCoroutine(this.getReadyToFire());
        }
        else if (!this.inPositionToFire && !this.bossAttack)
        {
            float angle = Vector3.Angle(this.transform.forward, Player.Instance.transform.position - this.transform.position);

            if (previousAngle < angle)
            {
                if (this.previousPreviousAngle > this.previousAngle)
                {
                    this.inPositionToFire = true;
                }
            }
            else
            {
                transform.Rotate(Vector3.up, Time.deltaTime * this.rotateSpeed);
            }
            this.previousPreviousAngle = this.previousAngle;
            this.previousAngle         = angle;
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// Handle Controller
    /// </summary>
    void FixedUpdate()
    {
        // Only run if controller is ready to start
        if (this.controller.CanMove)
        {
            this.shouldExecutePlan = true;
            this.setTarget(Player.Instance.transform.position);

            // Get Distance
            float distance = DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position);

            if (distance > this.laserSquareDistanceMin && distance < this.laserSquareDistanceMax)
            {
                if (this.lookingAtPlayer())
                {
                    // set controller to attack with laser
                    this.controller.AttackLaser = true;
                }
            }
            else if (distance < this.meleeSquareDistance)
            {
                if (this.lookingAtPlayer())
                {
                    // Set controller to attack with melee
                    this.controller.AttackMelee = true;
                }
            }
            else
            {
                // Set controller to move towards player
                this.controller.Walking = true;
                this.shouldExecutePlan  = true;
            }
        }
    }
Exemplo n.º 3
0
 /// <summary>
 /// Checks the plan and sees if it needs to be updated
 /// </summary>
 public override void checkPlan()
 {
     if (DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position) >= maxDistanceFromPlayer)
     {
         flyingTowardsPlayer = true;
     }
     // Check if plan is null or the square distance is to large
     if (this.shouldUpdatePlan())
     {
         base.resetTargetIndex();
         if (flyingTowardsPlayer)
         {
             this.getNewPlan(Player.Instance.transform.position);
             if (this.targetLocation == this.transform.position)
             {
                 flyingTowardsPlayer = false;
             }
         }
         else
         {
             this.updateTargetForMinimumDistance();
             this.getNewPlan(this.targetLocation);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// See if should destroy self
 /// </summary>
 private void testToDestroySelf()
 {
     if (this.CanDestroySelfOnMaxDistance && DistanceCalculator.squareEuclidianDistance(this.transform.position, this.targetLocation) > this.DestroySelfDistance)
     {
         // Kill self
         Destroy(this.gameObject);
     }
 }
 /// <summary>
 /// Check if plan needs to be updated
 /// </summary>
 /// <returns></returns>
 public override bool shouldUpdatePlan()
 {
     if (forceUpdatePlan)
     {
         forceUpdatePlan = false;
         return(true);
     }
     return(this.plan == null || DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position) < base.minMoveDistance);
 }
Exemplo n.º 6
0
 IEnumerator flyAwayFromPlayer()
 {
     while (DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position) <= Mathf.Pow(flyAwayDistance, 2f))
     {
         this.checkPlan();
         yield return(new WaitForSeconds(updatePlanBuffer));
     }
     StartCoroutine(this.flyToPlayer());
 }
    // Run through plan to execute moves
    public void executeCurrentPlan()
    {
        // If no plan is found, do nothing
        if (this.aiMovement != null)
        {
            if (this.targetIndex < this.plan.Count)
            {
                Vector3 targPos;

                if (this.vertType == AStar.VerticeType.GROUND)
                {
                    targPos = this.aiMovement.GetPlanetVertexNavigation().getVertex(this.plan[this.targetIndex]).position;
                }
                else
                {
                    targPos = this.aiMovement.GetPlanetVertexNavigation().flyingVertices[this.aiMovement.GetPlanetVertexNavigation().getVertex(this.plan[this.targetIndex]).key];
                }

                // Check if we've reached the target in the plan
                if (DistanceCalculator.squareEuclidianDistance(this.transform.position, targPos) < this.minReachDistance)
                {
                    // Increment to go to next target
                    ++this.targetIndex;
                }

                if (this.targetIndex < this.plan.Count)
                {
                    // move towards target in plan
                    this.move(targPos);
                }
            }
            else if (this.moveTowardsPlayerAtEndOfPath)
            {
                // Move towards the player
                this.move(Player.Instance.transform.position);
            }
            // No actions currently for when not supposed to reach th
        }
        else
        {
            // Check if past accepted errors
            if (this.errorsFound < this.errorsAccepted)
            {
                // Add to erros found
                ++this.errorsFound;

                // Get a new plan
                this.getNewPlan(this.targetLocation);
            }
            else
            {
                // Handle error
                this.handleError();
            }
        }
    }
Exemplo n.º 8
0
 /// <summary>
 /// Checks the plan and sees if it needs to be updated
 /// </summary>
 private void checkPlan()
 {
     // CHeck if plan is null or the square distance is to large
     if (this.plan == null || DistanceCalculator.squareEuclidianDistance(base.targetLocation, Player.Instance.transform.position) >= base.minReachDistance)
     {
         base.resetTargetIndex();
         base.setTarget(Player.Instance.transform.position);
         this.getNewPlan(base.targetLocation);
     }
 }
 IEnumerator AwayFromPlayer()
 {
     while (DistanceCalculator.squareEuclidianDistance(Player.Instance.transform.position, this.transform.position) <= asFarAs)
     {
         yield return(new WaitForSeconds(2f));
     }
     awayFromPlayer.enabled = false;
     towardsPlayer.enabled  = true;
     StartCoroutine(TowardsPlayer());
 }
Exemplo n.º 10
0
 IEnumerator flyToPlayer()
 {
     flyingTowardsPlayer = true;
     while (DistanceCalculator.squareEuclidianDistance(this.transform.position, Player.Instance.transform.position) >= (Mathf.Pow(flyingVertexHeight, 2) * proximityModifier))
     {
         this.checkPlan();
         yield return(new WaitForSeconds(updatePlanBuffer));
     }
     flyingTowardsPlayer = false;
     flyingBomb.dropBomb(Player.Instance.transform.position);
     StartCoroutine(this.flyAwayFromPlayer());
 }
Exemplo n.º 11
0
    /// <summary>
    /// time to wait before shake.
    /// </summary>
    private IEnumerator WaitTime()
    {
        // stop trigger
        this.triggerDisabled = true;

        // Previous positions
        float pastDistance    = 0;
        float currentDistance = 0;

        // Enable particles
        this.particles.enableEmission = true;

        do
        {
            // Set past
            pastDistance = currentDistance;

            // do wavey stuff
            sin += Time.deltaTime * shakeSize;

            // Shake stuff
            ShakeY();
            ShakeX();
            ShakeZ();

            // set current distance
            currentDistance = DistanceCalculator.squareEuclidianDistance(this.transform.position, this.startPos);

            // pause update
            yield return(new WaitForSeconds(Time.deltaTime));
        } while(currentDistance > pastDistance);

        // reset sin
        sin = 0;

        // disable particles
        this.particles.enableEmission = false;
//		this.particles.Pause();

        // set transform back to start
        this.transform.position = this.startPos;

        // start trigger
        this.triggerDisabled = false;
    }
Exemplo n.º 12
0
    void FixedUpdate()
    {
        if (DistanceCalculator.squareEuclidianDistance(this.transform.position, this.targetLocation) <= chargeDistanceThreshold)
        {
            isCharging = false;
        }

        if (isCharging)
        {
            this.moveSpeed = chargeSpeed;
        }
        else
        {
            this.moveSpeed = baseSpeed;
        }

        this.goliathAnimator.SetFloat("moveSpeed", this.moveSpeed);
        this.goliathAnimator.SetBool("isCharging", this.isCharging);
    }
Exemplo n.º 13
0
    /// <summary>
    /// Checks the plan and sees if it needs to be updated
    /// </summary>
    private void checkPlan()
    {
        // CHeck if plan is null or the square distance is to large
        if (this.plan == null || DistanceCalculator.squareEuclidianDistance(base.targetLocation, Player.Instance.transform.position) >= base.minReachDistance)
        {
            base.resetTargetIndex();

            if (flyingTowardsPlayer)
            {
                base.setTarget(Player.Instance.transform.position);
            }
            else
            {
                // TODO: Make this some vertices away from the player
                base.setTarget(Player.Instance.transform.position);
            }

            this.getNewPlan(base.getTarget());
        }
    }
Exemplo n.º 14
0
    public void setAverageVerticeLength()
    {
        // Set to low value
        float totalDistance    = 0;
        float verticesExplored = 0;

        // Search through each vertex
        for (int i = 0; i < this.vertices.Length; ++i)
        {
            // Check if vertice is null or not
            if (this.vertices[i] != Vector3.zero)
            {
                // Get connecting verts
                foreach (int vert in this.getMovesVertex(i))
                {
                    ++verticesExplored;
                    totalDistance += DistanceCalculator.squareEuclidianDistance(this.vertices[i], this.vertices[vert]);
                }
            }
        }

        // Calculate average and set square vertex length
        this.avgVertexlength = totalDistance / verticesExplored;
    }
Exemplo n.º 15
0
 public bool distanceCheck(Vector3 target, Vector3 currentPoint, float minChange)
 {
     return(DistanceCalculator.squareEuclidianDistance(target, currentPoint) >= minChange);
 }
Exemplo n.º 16
0
    /// <summary>
    /// get path plan
    /// </summary>
    private void GeneratePath(RaycastHit[] hits)
    {
        SystemLogger.write("Creating a path");

        // Create list to hold unformatted moves
        int[] unFormattedMoves = new int[3];

        // fill open list
        for (int i = 0; i < hits.Length; ++i)         //RaycastHit hit in hits
        {
            // Check if correct mesh was hit
            if (hits[i].triangleIndex != -1 && hits[i].collider != null && hits[i].collider.CompareTag("Planet"))
            {
                this.planetVertexNavigation.getMovesTriangle(hits[i].triangleIndex * 3, ref unFormattedMoves);
                break;
            }
        }

        // Add nodes to priority queue
        PriorityQueue queue = new PriorityQueue();

        // Create list of moves
        List <int> moves = new List <int>();

        for (int i = 0; i < unFormattedMoves.Length; ++i)
        {
            moves.Add(unFormattedMoves[i]);

            // Create new node
            AStarNode node = new AStarNode(1, 1, unFormattedMoves[i], moves);

            // add node to queue
            queue.addNode(node);

            moves.Clear();
        }

        // Visited nodes
        HashSet <int> visitedNodes = new HashSet <int>();

        // Run A* while moves available
        while (queue.Length() > 0)         // Is there an IsEmpty property... this may be better.
        {
            // Grab best node from priority queue
            AStarNode node = queue.popNode();

            // Get moves for a vertex
            List <int> availableMoves = this.planetVertexNavigation.getMovesVertex(node.Index);

            // Loop through the nodes available paths
            for (int i = 0; i < availableMoves.Count; ++i)
            {
                // Only use vertex if we haven't already explored this node
                if (!visitedNodes.Contains(availableMoves[i]))
                {
                    // Find obstacles
                    bool pointCollision;
                    if (this.movementType == VerticeType.GROUND)
                    {
                        pointCollision = !this.collisionAtPoint(this.planetVertexNavigation.getVertex(availableMoves[i]).position);
                    }
                    else
                    {
                        pointCollision = !this.collisionAtPoint(this.planetVertexNavigation.flyingVertices[this.planetVertexNavigation.getVertex(availableMoves[i]).key]);
                    }

                    // if obstacle was not found
                    if (pointCollision)
                    {
                        // Calculate distance between vertex and target
                        float vertexHeuristic = DistanceCalculator.squareEuclidianDistance(this.planetVertexNavigation.getVertex(availableMoves[i]).position, this.target);

                        // Check if close enough to target
                        if (vertexHeuristic < this.planetVertexNavigation.avgVertexlength * this.ExtraDistanceMultiplier)
                        {
                            SystemLogger.write("Path has been found");

                            // Yes we are, solved
                            this.plan = node.Moves;
                            this.plan.Add(availableMoves[i]);

                            // break out of loop
                            return;
                        }
                        else
                        {
                            // add to visited
                            visitedNodes.Add(availableMoves[i]);

                            // Create cropy of moves
                            List <int> newMoves = new List <int>(node.Moves);

                            // Add available moves
                            newMoves.Add(availableMoves[i]);

                            // Create new nodes with heuristic and step cost
                            AStarNode newNode = new AStarNode(node.G + this.calculateDistanceFromVertex(availableMoves[i]), vertexHeuristic, availableMoves[i], newMoves);

                            // add node to queue
                            queue.addNode(newNode);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 17
0
 /// <summary>
 /// Return  true if plan needs to be updated
 /// </summary>
 /// <returns></returns>
 public override bool shouldUpdatePlan()
 {
     return(this.plan == null || DistanceCalculator.squareEuclidianDistance(base.targetLocation, Player.Instance.transform.position) >= base.minMoveDistance);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Calculate the herustic using the given vertex
 /// </summary>
 /// <param name="vertex"></param>
 /// <returns></returns>
 private float calculateDistanceFromVertex(int vertex)
 {
     return(DistanceCalculator.squareEuclidianDistance(this.planetVertexNavigation.getVertex(vertex).position, this.target));
 }