相机移动 Y X Z 轴

2018-04-03  本文已影响0人  王一1

FixUpdate是放物理组件的,像RigidBody添加力等等

LateUpdate放相机的逻辑

Quaternion.identity是Quaternion(0,0,0,0) 旋转的初始角 

UnityEngine.Random.Range();的随机数值是左闭右开的,例求2~4之间的整数随机数:int a = UnityEngine.Random.Range(2,4+1);

相机移动实例:

脚本挂在Main Camera上

public Transform y_Axis;

    public Transform x_Axis;

    public Transform z_Axis;

    public Transform zoom_Axis;

    // 跟随的玩家

    public Transform player;

    // 鼠标的左右,上下偏移量

    private float ver,hor;

    //旋转速度

    public float roSpeed= 180;

    private float x;

    // 是否跟随玩家

    public bool isFollow;

    // 是否旋转玩家

    public bool isTurn;

    // 只放相机的逻辑

    private void LateUpdate()

    {

        hor = Input.GetAxis("Mouse X");

        ver = Input.GetAxis("Mouse Y");

        // 左右

        if (hor !=0)

        {

            y_Axis.Rotate(Vector3.up * hor * Time.deltaTime * roSpeed,Space.World);

        }

        // 上下

        if (ver!= 0)

        {

            x += -ver * Time.deltaTime * roSpeed;

            x = Mathf.Clamp(x, -20, 45);

            Quaternion q = Quaternion.identity;

            q = Quaternion.Euler(x, x_Axis.transform.eulerAngles.y, x_Axis.transform.eulerAngles.z);

            x_Axis.rotation = Quaternion.Lerp(x_Axis.rotation, q, Time.deltaTime * roSpeed);

          //  x_Axis.Rotate(transform.right*ver*Time.deltaTime*roSpeed,Space.World);

        }

        // 旋转玩家

        if (player !=null &&isTurn)

        {

            player.transform.forward = new Vector3(transform.forward.x,0,transform.forward.z);

        }

        // 跟随玩家

        if (player!=null &&isFollow )

        {

            y_Axis.position = Vector3.Lerp(y_Axis.position, player.position, Time.deltaTime * 10f);

        }

    }

上一篇下一篇

猜你喜欢

热点阅读