SmoothFollow.FollowUpdate C# (CSharp) Method

FollowUpdate() public method

public FollowUpdate ( ) : void
return void
    public void FollowUpdate()
    {
        // Early out if we don't have a target
        if (!target)
            return;

        // Calculate the current rotation angles
        float wantedRotationAngle = target.eulerAngles.y;
        float wantedHeight = target.position.y + height;

        float currentRotationAngle = transform.eulerAngles.y;
        float currentHeight = transform.position.y;

        float deltaAngle = wantedRotationAngle - currentRotationAngle;
        if (deltaAngle > 180.0f) deltaAngle -= 360;
        else if (deltaAngle < -180.0f) deltaAngle += 360;

        if (deltaAngle > 90) deltaAngle = 180 - deltaAngle;
        if (deltaAngle < -90) deltaAngle = -180 - deltaAngle;

        // Damp the rotation around the y-axis
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, currentRotationAngle+deltaAngle, rotationDamping * Time.deltaTime);

        // Damp the height
        currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

        // Convert the angle into a rotation
        Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
        //Quaternion r = Quaternion.Euler(0, rotate, 0);
        //float currentRotation = 1;

        // Set the position of the camera on the x-z plane to:
        // distance meters behind the target
        transform.position = target.position;

        transform.position -= currentRotation * Vector3.forward * distance;

        // Set the height of the camera
        transform.position = new Vector3(transform.position.x,currentHeight,transform.position.z);

        // Always look at the target
        transform.LookAt(target);
    }

Usage Example

    void OnJoystickMove(MovingJoystick move)
    {
        hasDes = false;
        float joyPositionX = move.joystickAxis.x;
        float joyPositionY = move.joystickAxis.y;

        if (move.joystickName == "MoveJoystick")
        {
            if (joyPositionY != 0 || joyPositionX != 0)
            {
                //设置角色的朝向(朝向当前坐标+摇杆偏移量)
                Quaternion r = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);

                Vector3 dir = r * new Vector3(joyPositionX, 0, joyPositionY);
                transform.LookAt(new Vector3(transform.position.x + dir.x, transform.position.y, transform.position.z + dir.z));

                //移动玩家的位置(按朝向位置移动)
                //transform.Translate(Vector3.forward * Time.deltaTime * 5);
                Move();

                //移动摄像机
                sf.FollowUpdate();
                //播放奔跑动画
                animator.speed = 2.0f;
                animator.SetFloat("Speed", 1.0f);
            }
        }
    }
All Usage Examples Of SmoothFollow::FollowUpdate