2017.12.17笔记-关于角色和相机的控制

来源:互联网 发布:阅读杂志的软件 编辑:程序博客网 时间:2024/06/06 09:05

利用刚体的velocity进行移动,缺点:只能以世界坐标轴进行移动,暂时没发现velocity下用局部坐标系移动的方法:

    void VMove()    {        if (Input.GetAxisRaw("Horizontal") > 0)        {            moveControl.velocity = new Vector3(speed, moveControl.velocity.y, moveControl.velocity.z);        }        else if (Input.GetAxisRaw("Horizontal") < 0)        {            moveControl.velocity = new Vector3(-speed, moveControl.velocity.y, moveControl.velocity.z);        }        else        {            moveControl.velocity = new Vector3(0, moveControl.velocity.y, moveControl.velocity.z);        }        if (Input.GetAxisRaw("Vertical") > 0)        {            moveControl.velocity = new Vector3(moveControl.velocity.x, moveControl.velocity.y, speed);        }        else if (Input.GetAxisRaw("Vertical") < 0)        {            moveControl.velocity = new Vector3(moveControl.velocity.x, moveControl.velocity.y, -speed);        }        else        {            moveControl.velocity = new Vector3(moveControl.velocity.x, moveControl.velocity.y, 0);        }    }



局部坐标移动方法:

    void Move()    {        if (Input.GetAxisRaw("Horizontal") > 0)        {            this.transform.Translate(Vector3.right * speed*Time.deltaTime,Space.Self);        }        else if (Input.GetAxisRaw("Horizontal") < 0)        {            this.transform.Translate(Vector3.left * speed * Time.deltaTime, Space.Self);        }        else        {            this.transform.Translate(Vector3.zero);        }        if (Input.GetAxisRaw("Vertical") > 0)        {            this.transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);        }        else if (Input.GetAxisRaw("Vertical") < 0)        {            this.transform.Translate(Vector3.back * speed * Time.deltaTime, Space.Self);        }        else        {            this.transform.Translate(Vector3.zero);        }    }


鼠标右键转动视野方法:

网上查得的方法如下

    if (Input.GetMouseButton(1))    {        //根据鼠标移动的快慢(增量), 获得相机左右旋转的角度(处理X)          float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;        //根据鼠标移动的快慢(增量), 获得相机上下旋转的角度(处理Y)          rotationY += Input.GetAxis("Mouse Y") * sensitivityY;        //角度限制. rotationY小于min,返回min. 大于max,返回max. 否则返回value           rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);        //总体设置一下相机角度          transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);    }

如果只挂载到camera下的话,不满足角色移动需求,研究后修改代码如下:

物体层级关系:胶囊为camera的父物体。胶囊上挂载PlayerControl。camera上挂载CameraControl 

PlayerControl控制旋转代码:

    if (Input.GetMouseButton(1))    {        //根据鼠标移动的快慢(增量), 获得相机左右旋转的角度(处理X)          float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;        //总体设置一下相机角度          transform.localEulerAngles = new Vector3(0, rotationX, 0);    }

CameraControl控制旋转代码:

        if (Input.GetMouseButton(1))        {            //根据鼠标移动的快慢(增量), 获得相机上下旋转的角度(处理Y)              rotationY += Input.GetAxis("Mouse Y") * sensitivityY;            //角度限制. rotationY小于min,返回min. 大于max,返回max. 否则返回value               rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);            //总体设置一下相机角度              transform.localEulerAngles = new Vector3(-rotationY, 0, 0);        }

这样一来,配合局部坐标系移动方法,使得角色移动视角控制类似与WOW游戏人物的控制


关于鼠标滚轮拉近拉远视角,网上所示的更改camera视野方法,更适用于狙击枪的瞄准模式,如下:

        float fov = playerCamera.fieldOfView;        fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity1;        fov = Mathf.Clamp(fov, minFov, maxFov);        playerCamera.fieldOfView = fov;

接下来要解决的问题:滚轮拉近拉远角色方法以及配合右键旋转,还有就是按住左键绕角色旋转且不旋转角色本身。


原创粉丝点击