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 }
  • 相关阅读:
    Hadoop学习:Map/Reduce初探与小Demo实现
    OCR OneNote
    百度OCR识别示例
    Sqlserver 中添加数据库登陆账号并授予数据库所有者权限
    IIs 中运行asp程序出现“An error occurred on the server when processing the URL. Please contact the system administrator.”错误
    SqlServer 删除重复记录
    Chrome 制作绿色便携版
    Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
    C# 中如何将List<string>里的集合转换成字符串并按指定的字符进行分隔?
    使用SQL SERVER FOR XML PATH将多个结果集转换成一行并进行去重处理
  • 原文地址:https://www.cnblogs.com/SeaSwallow/p/6568477.html
Copyright © 2011-2022 走看看