zoukankan      html  css  js  c++  java
  • Unity 摄像机旋转跟随缩放控制

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class CameraController : MonoBehaviour
     6 {
     7 
     8     [SerializeField]
     9     private float distance = 5f;    //跟随距离
    10     [SerializeField]
    11     private float speed = 2f;   //跟随速率
    12     [SerializeField]
    13     private float mouseSpeed = 2f;  //鼠标移动敏感度
    14     [SerializeField]
    15     private float mouseScroll = 2f; //鼠标缩放速率
    16 
    17     //跟随最大与最小距离
    18     private float distanceMin = 2f;
    19     private float distanceMax = 15f;
    20     private Transform target;   //摄像机跟随的目标
    21     private Vector3 targetPos;  //目标位置
    22     private Vector3 direction = Vector3.zero;   //摄像机移动的方向
    23     private float mouseX = 0f;  //鼠标x轴移动值
    24     private float mouseY = 15f; //鼠标y轴移动值
    25     private float mouseYMin = -5f;   //鼠标y轴移动值最小值
    26     private float mouseYMax = 89.5f;    //鼠标y轴移动值最大值
    27 
    28 
    29     private void Awake()
    30     {
    31         target = GameObject.FindWithTag("Player").transform;
    32         direction = target.position - transform.position;
    33     }
    34 
    35     private void LateUpdate()
    36     {
    37         GetInput();
    38 
    39         CameraRotate();
    40 
    41         CalculateTargetPos();
    42 
    43         FollowTarget();
    44     }
    45 
    46     private void CalculateTargetPos()
    47     {
    48         //文章入口的代码图解
    49         targetPos = target.position - direction.normalized * distance;
    50     }
    51 
    52     private void FollowTarget()
    53     {
    54         transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * speed);
    55         transform.LookAt(target.position);
    56     }
    57 
    58     private void GetInput()
    59     {
    60         if (Input.GetMouseButton(0))
    61         {
    62             Cursor.visible = false;
    63 
    64             mouseX += Input.GetAxis("Mouse X") * mouseSpeed;
    65             //摄像机鼠标上下移动与mouseY是相反的,可自己通过观察可得,这里就不明说了
    66             mouseY -= Input.GetAxis("Mouse Y") * mouseSpeed;
    67 
    68             mouseY = Mathf.Clamp(mouseY, mouseYMin, mouseYMax);
    69 
    70         }
    71         else Cursor.visible = true;
    72 
    73         distance = distance - Input.GetAxis("Mouse ScrollWheel") * mouseScroll;
    74         distance = Mathf.Clamp(distance, distanceMin, distanceMax);
    75 
    76     }
    77 
    78     private void CameraRotate()
    79     {
    80         Quaternion rotation = Quaternion.Euler(mouseY, mouseX, 0);
    81         //transform.rotation = rotation;    //不围绕目标旋转
    82         //Quaternion作用于Vector3的右乘操作(*)返回一个将向量做旋转操作后的向量.
    83         //这里算是一个难点,这里意思是将根据鼠标XY轴旋转“加到”Vector3.forward向前向量,所以direction是经过了旋转的向前向量
    84         //就好像一条直线绕着一点旋转了一样
    85         direction = rotation * Vector3.forward;
    86     }
    87 
    88 }
  • 相关阅读:
    js直接获取当前windows登陆账号---仅适用于IE
    Windows变量路径与通配符
    IDEA数据库生成Entity 带注释, 默认值
    BIM+区块链在建筑业施工过程结算的应用
    Java中动态规则的实现方式
    Git查看本地仓库关联关系以及清理无效远程分支
    Golang把字符串数组、[]interface{}打乱、切片乱序
    Golang将map数组按照指定字段排序
    Golang把时间和数字相乘报错invalid operation: second * time.Second (mismatched types int and time.Duration)
    Golang获取明日时间及距离明日凌晨的时间差
  • 原文地址:https://www.cnblogs.com/SeaSwallow/p/6568477.html
Copyright © 2011-2022 走看看