コード例 #1
0
    //Compute a random spawn position around organism
    protected virtual Vector2 ComputeRandomSpawnPosAround()
    {
        spawnPos  = Random.insideUnitCircle.normalized;
        spawnPos *= selfOrganism.GetOrganismSize() + 1f;
        spawnPos += (Vector2)transform.position;

        return(spawnPos);
    }
コード例 #2
0
    /***** TARGET MOVEMENTS FUNCTIONS *****/

    // Move organism towards targetOrganism
    private void MoveTowardsTargetOrganism()
    {
        moveDirection = targetOrganism.transform.position - transform.position;

        // Stick to target but doesn't push it
        if (moveDirection.magnitude > (selfOrganism.GetOrganismSize() / 2f + targetOrganism.GetOrganismSize() / 2f) + 0.1f)
        {
            // Normalize direction before applying force to keep force constant
            moveDirection.Normalize();

            MoveOrganism(false);
        }
    }
コード例 #3
0
    /*** MOVEMENTS FUNCTIONS ***/

    // Move the player from input
    private void MovePlayer()
    {
        if (fireTarget)
        {
            // Compute values used to repeal player from target
            oppositeForceDirection = transform.position - fireTarget.transform.position;
            targetDistance         = oppositeForceDirection.magnitude - (fireTarget.GetOrganismSize() / 2);
        }

        if (fireTarget && targetDistance < minRange)
        {
            // Apply opposite force if player too close from target
            rb.AddForce(oppositeForceDirection.normalized * speed * (1f - targetDistance / minRange), ForceMode2D.Impulse);
        }
        else
        {
            // Apply a force to move the player in movementDirection
            rb.AddForce(moveDirection * speed, ForceMode2D.Impulse);
        }

        // Clamp the player velocity to not go too fast
        rb.velocity = Vector2.ClampMagnitude(rb.velocity, currentMaxVelocity);
    }