码迷,mamicode.com
首页 > 编程语言 > 详细

unity的摄像机脚本

时间:2019-09-08 20:06:34      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:arp   cto   unit   des   nsf   void   upd   mon   unity   

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class MainCamera : MonoBehaviour
{
    public Transform target;
    public float targetHeight = 1.2f;
    public float distance = 8.0f;
    public float offsetFromWall = 0.1f;

    public float maxDistance = 15;
    public float minDistance = 3f;

    public float xSpeed = 200.0f;
    public float ySpeed = 200.0f;

    public int yMinLimit = 0;
    public int yMaxLimit = 80;

    public int zoomRate = 40;

    public float rotationDampening = 3.0f;
    public float zoomDampening = 5.0f;

    public LayerMask collisionLayers = -1;

    private float xDeg = -53.2f;
    private float yDeg = 22.4f;
    private float currentDistance;
    private float desiredDistance;
    private float correctedDistance;

    void Start()
    {
        currentDistance = distance; //当前距离
        desiredDistance = distance; //默认距离
        correctedDistance = distance; //需要修正的距离
    }

    void LateUpdate()
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;
        Vector3 vTargetOffset;
        if (!target)
        {
            return;
        }
        if (Input.GetMouseButton(1))
        {
            xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        }
        yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
        xDeg = ClampAngle(xDeg, -360, 360);
        Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);

        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); //滚珠移动摄像头与人的距离
        correctedDistance = desiredDistance;

        vTargetOffset = new Vector3(0, -targetHeight, 0);
        Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); //摄像机与人之间的向量

        RaycastHit collisionHit;
        Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z); //修改人身上的射线发出点坐标

        bool isCorrected = false;
        if (Physics.Linecast(trueTargetPosition, position, out collisionHit, collisionLayers.value))
        {
            correctedDistance = Vector3.Distance(trueTargetPosition, collisionHit.point) - offsetFromWall;
            isCorrected = true;
        }

        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
        currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
        position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
        transform.rotation = rotation;
        transform.position = position;
    }

    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}

  

unity的摄像机脚本

标签:arp   cto   unit   des   nsf   void   upd   mon   unity   

原文地址:https://www.cnblogs.com/allyh/p/11487794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!