tps(第三人称射击游戏) 1.相机

tps(第三人称射击游戏)
1.相机

相机跟随主角并绑定相机位置:

 cursor_y = Input.GetAxis("Mouse Y");
        /** 设定相机位置  */
        distanceup -= cursor_y * 0.05f;  //上下移动反方向移动相机
        transform.position = target.position - target.forward * distanceAway + target.up* distanceup;
        if (transform.position.y <= 0)
            transform.position = new Vector3(transform.position.x, 0, transform.position.z);

Input.GetAxis(“Mouse Y”);为鼠标在屏幕上移动时y轴坐标的变换
这段代码实现粗略的相机绑定人物位置并处于人物的后上方,其中target就是人物并且在视角上下转动的时候往相反方向移动相机,且在相机即将到达地下时,限定y坐标为0;

相机随鼠标移动改变面朝方向:

 /** 相机视角随鼠标运动转动  */
        rotationX = transform.localEulerAngles.y+ Input.GetAxis("Mouse X") * sensitivityX ;
        rotationY += cursor_y * sensitivityY;
        transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);

相机视角在x轴上旋转的角度加上本身相对于世界的欧拉角就是新的y轴欧拉角,y轴由于相机
的初始欧拉角为0所以只用了鼠标的坐标变换。

改变角色面朝的方向,并让枪指向屏幕中央

 /**   主角面朝方向   */
        target.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0)  ;
        gun.transform.localEulerAngles = new Vector3(-rotationY,0, 0);

由于主角不存在y轴上的旋转所以,x轴上的欧拉角为0

猜你喜欢

转载自blog.csdn.net/qq_38105943/article/details/78577458