封装相机用于节点间的移动

 

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

public class Camera_mover : MonoBehaviour
{
    //相机有两个父物体
    public Transform camera_root1;
    public Transform camera_root2;
    public Camera camera_my;

    public float speed_move=2;
    public float speed_rotate=10;
    public float speed_dis_z = 1;

    private float rotateX;
    private float rotateY;
    private float z_offet;
    float velocityX = 0.0f;
    float velocityY = 0.0f;
    float velocitz = 0.0f;

    //移动过程中禁止转向
    public bool lock_camera;
    public Transform target;
    private Transform target_point;
    public Transform[] trans;
    // Start is called before the first frame update
    void Start()
    {
        target_point = target;
        lock_camera = true;
    }

    // Update is called once per frame
    void Update()
    {
        if(target_point!= target)
        {
            target_point = target;
            lock_camera = true;
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            target = trans[0];
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            target = trans[1];
        }

    }
    private void LateUpdate()
    {
        if (target == null) return;
        if (lock_camera)
        {
            camera_root1.Translate((target_point.position - camera_root1.position) * Time.deltaTime * speed_move, Space.World);
            camera_root1.rotation = Quaternion.Lerp(camera_root1.rotation, Quaternion.Euler(0, target_point.eulerAngles.y, 0), Time.deltaTime * speed_move);
            camera_root2.localRotation = Quaternion.Lerp(camera_root2.localRotation, Quaternion.Euler(target_point.eulerAngles.x, 0, 0), Time.deltaTime * speed_move);
            if (z_offet < -10)
            {
                z_offet += Time.deltaTime * speed_dis_z;
                if (z_offet >= -10)
                {
                    z_offet = -10;
                }
            }
            else
            {
                z_offet -= Time.deltaTime * speed_dis_z;
                if (z_offet <= -10)
                {
                    z_offet = -10;
                }
            }
            camera_my.transform.localPosition = new Vector3(0, 0, z_offet);
            if (Vector3.Distance(camera_root1.position, target.position) <= 0.05f)
            {
                //停
                lock_camera = false;
                camera_my.fieldOfView = 60;
                camera_root1.rotation = Quaternion.Euler(0, target_point.eulerAngles.y, 0);
                camera_root2.localRotation = Quaternion.Euler(target_point.eulerAngles.x, 0, 0);

                rotateX = camera_root1.rotation.eulerAngles.y;
                rotateY = camera_root2.eulerAngles.x > 90 ? camera_root2.eulerAngles.x - 360 : camera_root2.eulerAngles.x;
            }
        }
        else
        {
            if (Input.GetMouseButton(1))
            {
                velocityX= Input.GetAxis("Mouse X") * speed_rotate;
                velocityY = Input.GetAxis("Mouse Y") * speed_rotate;
            }
            velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * 5);
            velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * 5);
            rotateX +=  velocityX;
            rotateY +=  velocityY;
            rotateY = Mathf.Clamp(rotateY, -80, 80);
            camera_root1.transform.rotation = Quaternion.Euler(0, rotateX, 0);
            camera_root2.transform.localRotation = Quaternion.Euler(-rotateY, 0, 0);

            velocitz = Input.GetAxis("Mouse ScrollWheel") * speed_dis_z;
            z_offet += velocitz;
            z_offet = Mathf.Clamp(z_offet, -15, 1);
            camera_my.transform.localPosition = new Vector3(0, 0, z_offet);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/fanfan_hongyun/article/details/127280435