Unity3D、Camera跟随人物、围绕物体旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour {
    public float distance = 0;
    public float scrollSpeed = 10;
    public float rotateSpeed = 2;

    private Transform player;
    private Vector3 offSetPostion;//镜头位置偏移量
    private bool isRotating = false;//鼠标水平滑动


    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
        transform.LookAt(player.position);
        offSetPostion = transform.position - player.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = offSetPostion + player.position;
        //处理视野的旋转
        RotateView();
        //处理视野的拉近拉远效果
        ScrollView();

    }

    void ScrollView()
    {
        distance = offSetPostion.magnitude; //返回向量的长度
        distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;   //返回虚拟轴的值Mouse ScrollWheel:鼠标滑轮
        distance = Mathf.Clamp(distance, 8, 18);//限制镜头最近和最远距离
        offSetPostion = offSetPostion.normalized * distance;
    }

    void RotateView()
    {
        //Input.GetAxis("Mouse X");//得到鼠标在水平方向上的滑动
        // Input.GetAxis("Mouse Y");//得到鼠标在垂直方向上的滑动

        if (Input.GetMouseButtonDown(1))
        {
            isRotating = true;
        }

        if (Input.GetMouseButtonUp(1))
        {
            isRotating = false;
        }

        if (isRotating)
        {

            Vector3 originalPos = transform.position;
            Quaternion originalRotation = transform.rotation;

            transform.RotateAround(player.position, player.up, rotateSpeed * Input.GetAxis("Mouse X"));
            transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));
            float x = transform.eulerAngles.x;

            if (x > 80 || x < 35)
            {
                transform.position = originalPos;
                transform.rotation = originalRotation;
            }
        }
        offSetPostion = transform.position - player.position;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容