コード例 #1
0
    void ShadowPlayerUpdate()
    {
        // maybe change logic here?
        destination = player.position + targetDirectionFromPlayer * distanceToMaintain;

        Vector3 movement = destination - this.transform.position;

        movement = movement.normalized * moveSpeed * shadowSpeedCoefficient * Time.fixedDeltaTime;

        // if we're close enough, don't overshoot
        if (UsefulStuff.CloseEnough(this.transform.position, destination, movement.sqrMagnitude))
        {
            movement = destination - this.transform.position;
        }
        else if (UsefulStuff.FarEnough(this.transform.position, destination, tooFarAway * tooFarAway))
        {
            BeginCatchUpToPlayer();
        }

        base.MoveWithSliding(
            new Vector3(
                movement.x,
                movement.y,
                0));
    }
コード例 #2
0
    protected void MoveWithSliding(Vector3 movement)
    {
        characterController.Move(
            new Vector3(
                movement.x,
                movement.y,
                0));

        // if we're only moving vertical,
        // see if we can slide on a slope
        if (movement.y != 0 && movement.x == 0)
        {
            if (this.transform.position == previousPosition)
            {
                // try going right
                characterController.Move(
                    new Vector3(
                        diagonalSpeedModifier * moveSpeed * Time.fixedDeltaTime,
                        0,
                        0));

                // we didn't slide, so this is correct
                if (UsefulStuff.CloseEnough(this.transform.position.y, previousPosition.y, 0.01f))
                {
                    characterController.Move(
                        new Vector3(
                            0,
                            movement.y * Time.fixedDeltaTime,
                            0));

                    // if we didn't move, we must be hitting a wall
                    if (UsefulStuff.CloseEnough(this.transform.position.y, previousPosition.y, 0.01f))
                    {
                        this.transform.position = previousPosition;
                    }
                }
                else
                {
                    this.transform.position = previousPosition;

                    // try going left
                    characterController.Move(
                        new Vector3(
                            -diagonalSpeedModifier * moveSpeed * Time.fixedDeltaTime,
                            0,
                            0));

                    // we didn't slide, so this is correct
                    if (UsefulStuff.CloseEnough(this.transform.position.y, previousPosition.y, 0.01f))
                    {
                        characterController.Move(
                            new Vector3(
                                0,
                                movement.y * Time.fixedDeltaTime,
                                0));

                        // if we didn't move, we must be hitting a wall
                        if (UsefulStuff.CloseEnough(this.transform.position.y, previousPosition.y, 0.01f))
                        {
                            this.transform.position = previousPosition;
                        }
                    }
                    else
                    {
                        this.transform.position = previousPosition;
                    }
                }
            }
        }
    }