内容纲要

相机自由旋转角度拉近拉远视角

记录玩家与相机的向量值

transform.position = player.position + offset;
        float xDeg = 0;
        float yDeg = 0;
        if (Input.GetMouseButton(1))
        {//右键跟随玩家背后
            xDeg = -Input.GetAxis("Mouse Y") * rotSpeed.x * Time.deltaTime;
            yDeg = player.rotation.eulerAngles.y - transform.rotation.eulerAngles.y;
        }
        else if(Input.GetMouseButton(0))
        {
            //左键绕玩家自由旋转
            xDeg = -Input.GetAxis("Mouse Y") * rotSpeed.x * Time.deltaTime;
            yDeg = Input.GetAxis("Mouse X") * rotSpeed.y * Time.deltaTime;

        }
        transform.RotateAround(player.position, transform.right, xDeg);
        transform.RotateAround(player.position, player.up, yDeg);
        //转完以后跟新offset
        offset = transform.position - player.position;
        float delta= Input.GetAxis("Mouse ScrollWheel")*scrollSpeed*
            Time.deltaTime;
        if (delta != 0 && fps)
        {
            fps = false;
            offset = -transform.forward;
        }
        else if (Mathf.Abs(offset.magnitude) <= 1f)//取绝对值,切换至第一人称
        {
            fps = true;
            offset = Vector3.zero;
        }
        Vector3 offestNorm= offset.normalized;//单位化向量
        float offsetLen= offset.magnitude;
        offsetLen -= delta;
        offset = offestNorm * offsetLen;//计算得到新的向量
    }

Blend

混合动画树,使用简单,相比之前的动画系统方便


  private Animator anim;

    void Start()
    {
        anim = GetComponent();    
    }

  float forward = 0;
        float turn = 0;
        if (v {-5,5}超出范围

        if (Input.GetMouseButton(1))
        {
            turn = mouseX * rotSpeed;
            //this.transform.Rotate(0,turn, 0, Space.Self);//局部坐标系移动
        }
        turn += h * rotSpeed;//{-360,360}/360={-1,1}=>{左半圈,右半圈}
        //h={-1,1},mousex={-1,1},rootspeed=180

        //执行语句
        //this.transform.Translate(0, 0, forward, Space.Self);
        //this.transform.Rotate(0,turn, 0, Space.Self);
        anim.SetFloat("Forward", forward/moveSpeed,0.5f,Time.deltaTime);//缓冲传导,更平滑,0.5f,Time.deltaTime
        anim.SetFloat("Turn", turn/360);//

发表评论