示例#1
0
    void moveToTarget()
    {
        rb.constraints = RigidbodyConstraints2D.FreezeRotation;

        Vector2 arriveAccel = steeringUtils.arrive(arriveTarget);

        steeringUtils.steer(arriveAccel);
        steeringUtils.lookWhereYoureGoing();
    }
示例#2
0
    void FixedUpdate()
    {
        if (enemy.stunTill > Time.time)
        {
            GetComponent <Rigidbody2D>().velocity = Vector2.zero;
            return;
        }

        float characterOrientation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;

        /* Update the wander orientation */
        updateWanderOrientation(characterOrientation);

        /* Calculate the combined target orientation */
        float targetOrientation = wanderOrientation + characterOrientation;

        /* Calculate the center of the wander circle */
        Vector3 targetPosition = transform.position + (orientationToVector(characterOrientation) * wanderOffset);

        //		debugRing.transform.position = targetPosition;

        /* Calculate the target position */
        targetPosition = targetPosition + (orientationToVector(targetOrientation) * wanderRadius);

        //		Debug.DrawLine (transform.position, targetPosition);


        Vector2 acceleration = steeringUtils.seek(targetPosition);

        steeringUtils.steer(acceleration);

        steeringUtils.lookWhereYoureGoing();
    }
示例#3
0
    void FixedUpdate()
    {
        float        dist      = Vector3.Distance(transform.position, player.position);
        Vector3      direction = player.position - transform.position;
        RaycastHit2D hit       = Physics2D.Raycast(transform.position, direction);

        if (dist > atkDist || hit.collider == null || hit.collider.tag != "Player")
        {
            laser.stop();

            findPathToPlayer();

            if (currentPath != null)
            {
                Vector2 accel = followPath.getSteering(currentPath);
                steeringUtils.steer(accel);
                steeringUtils.lookWhereYoureGoing();
            }
            // If we have not path to the player then stand still
            else
            {
                rb.velocity = Vector2.zero;
            }
        }
        else
        {
            laser.fire(hit);

            rb.velocity = Vector2.zero;
            steeringUtils.lookAtDirection(direction);
        }
    }
示例#4
0
文件: Wander.cs 项目: frms/game-jams
    void FixedUpdate()
    {
        float characterOrientation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;

        /* Update the wander orientation */
        wanderOrientation += randomBinomial() * wanderRate;

        /* Calculate the combined target orientation */
        float targetOrientation = wanderOrientation + characterOrientation;

        /* Calculate the center of the wander circle */
        Vector3 targetPosition = transform.position + (orientationToVector(characterOrientation) * wanderOffset);

        //debugRing.transform.position = targetPosition;

        /* Calculate the target position */
        targetPosition = targetPosition + (orientationToVector(targetOrientation) * wanderRadius);

        //Debug.DrawLine (transform.position, targetPosition);


        Vector2 acceleration = steeringUtils.seek(targetPosition);

        steeringUtils.steer(acceleration);

        steeringUtils.lookWhereYoureGoing();
    }
示例#5
0
    // Update is called once per frame
    void Update()
    {
        if (enemy.stunTill > Time.time)
        {
            attacking = false;
            GetComponent <Rigidbody2D>().velocity = Vector2.zero;
            return;
        }

        if (player != null)
        {
            Vector2 sepAccel    = steeringUtils.separation("Enemy");
            Vector2 arriveAccel = steeringUtils.arrive(player.position);

            if (sepAccel != Vector2.zero)
            {
                steeringUtils.steer(sepAccel);
            }
            else if (arriveAccel != Vector2.zero)
            {
                steeringUtils.steer(arriveAccel);
            }

            attacking = (Vector3.Distance(transform.position, player.position) <= steeringUtils.targetRadius);
        }
        // Else the player is dead so stop attacking and stop moving
        else
        {
            attacking = false;
            GetComponent <Rigidbody2D>().velocity = Vector2.zero;
        }

        updateMeleeAttack();

        steeringUtils.lookWhereYoureGoing();
    }
示例#6
0
文件: Mover.cs 项目: frms/game-jams
    internal void moveUnit()
    {
        if (target != null)
        {
            findPathToUnit();
        }

        Vector2 accel = Vector2.zero;

        bool standStill = false;

        if (currentPath != null)
        {
            Vector2 targetPosition;

            accel = steerTowardsPath(out targetPosition);

            int[] mapPos = Map.map.worldToMapPoint(targetPosition);

            if (!equals(mapPos, reservedPos))
            {
                if (Map.map.getObj(mapPos) == null)
                {
                    Map.map.setObj(reservedPos, null);
                    Map.map.setObj(mapPos, this);
                    reservedPos = mapPos;
                }
                else
                {
                    //Debug.Log (name + " is moving onto an occupied node");

                    int i = findNextUnoccupiedNode(targetPosition);

                    // Should prob change the if to find path to target and get as close as possible
                    // and make the else code happen outside of the else block
                    if (i == currentPath.Length)
                    {
                        //Debug.Log(name + " has no unoccupied nodes on its current path to its goal");
                        standStill = true;
                    }
                    else
                    {
                        Vector3 startPos = Map.map.mapToWorldPoint(reservedPos[0], reservedPos[1]);
                        Vector3 endPos   = currentPath[i];

                        LinePath detour = AStar.findPath(Map.map, startPos, endPos, null, 0, false);

                        /* If we can't find a detour path just find a way to the end node */
                        if (detour == null)
                        {
                            //Debug.Log (name + " no detour to next open space. Finding new path to end goal all together.");
                            currentPath = AStar.findPath(Map.map, currentPath[0], currentPath.endNode, null, 0, false);
                        }
                        /* Else update the current path */
                        else
                        {
                            //Vector3[] newNodes = new Vector3[detour.Length + (currentPath.Length - i - 1)];

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

                            for (int j = 0; j < currentPath.Length; j++)
                            {
                                if (currentPath[j] != startPos)
                                {
                                    newNodes.Add(currentPath[j]);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            for (int j = 0; j < detour.Length; j++)
                            {
                                newNodes.Add(detour[j]);
                            }

                            i = i + 1;
                            for (; i < currentPath.Length; i++)
                            {
                                newNodes.Add(currentPath[i]);
                            }

                            currentPath = new LinePath(newNodes.ToArray());
                        }

                        accel = steerTowardsPath(out targetPosition);
                    }
                }
            }
        }
        else
        {
            standStill = true;
        }

        if (standStill)
        {
            accel = steeringUtils.arrive(Map.map.mapToWorldPoint(reservedPos [0], reservedPos [1]));
        }

        steeringUtils.steer(accel);
        steeringUtils.lookWhereYoureGoing();
    }