服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现场景漫游相机

Unity实现场景漫游相机

2022-10-12 13:33SlowFeather C#

这篇文章主要为大家详细介绍了Unity实现场景漫游相机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下

前言

拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你的不二选择
代码里加了注释,改起来也很方便。

使用方法

把脚本拖拽到场景相机上,开箱即用。

  • WASD前后左右移动
  • QE为上下
  • Shift加速
  • 鼠标右键按住旋转视角
  • ESC退出游戏

源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
 using UnityEngine.InputSystem;
 using UnityEngine.InputSystem.Controls;
#endif
 
using UnityEngine;
 
 public class SimpleCameraController : MonoBehaviour
 {
  #region 相机状态
  /// <summary>
  /// 相机状态
  /// </summary>
  class CameraState
  {
   public float yaw;
   public float pitch;
   public float roll;
   public float x;
   public float y;
   public float z;
 
   public void SetFromTransform(Transform t)
   {
    pitch = t.eulerAngles.x;
    yaw = t.eulerAngles.y;
    roll = t.eulerAngles.z;
    x = t.position.x;
    y = t.position.y;
    z = t.position.z;
   }
 
   public void Translate(Vector3 translation)
   {
    Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
 
    x += rotatedTranslation.x;
    y += rotatedTranslation.y;
    z += rotatedTranslation.z;
   }
 
   public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
   {
    yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
    pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
    roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
    
    x = Mathf.Lerp(x, target.x, positionLerpPct);
    y = Mathf.Lerp(y, target.y, positionLerpPct);
    z = Mathf.Lerp(z, target.z, positionLerpPct);
   }
 
   public void UpdateTransform(Transform t)
   {
    t.eulerAngles = new Vector3(pitch, yaw, roll);
    t.position = new Vector3(x, y, z);
   }
  }
  #endregion
 
  CameraState m_TargetCameraState = new CameraState();
  CameraState m_InterpolatingCameraState = new CameraState();
 
  [Header("Movement Settings 移动设置")]
  [Tooltip("Exponential boost factor on translation, controllable by mouse wheel. 平移的指数增强因子,可通过鼠标滚轮控制。")]
  public float boost = 3.5f;
 
  [Tooltip("Time it takes to interpolate camera position 99% of the way to the target. 将相机位置插值到目标位置99%所需的时间。"), Range(0.001f, 1f)]
  public float positionLerpTime = 0.2f;
 
  [Header("Rotation Settings 旋转设定")]
  [Tooltip("X = Change in mouse position. 改变鼠标位置。\nY = Multiplicative factor for camera rotation. 相机旋转的乘性因子。")]
  public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
 
  [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target. 插值相机旋转99%到目标所需的时间。"), Range(0.001f, 1f)]
  public float rotationLerpTime = 0.01f;
 
  [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否将鼠标输入的Y轴反转为旋转。")]
  public bool invertY = false;
 
  void OnEnable()
  {
   m_TargetCameraState.SetFromTransform(transform);
   m_InterpolatingCameraState.SetFromTransform(transform);
  }
 
  Vector3 GetInputTranslationDirection()
  {
   Vector3 direction = new Vector3();
   if (Input.GetKey(KeyCode.W))
   {
    direction += Vector3.forward;
   }
   if (Input.GetKey(KeyCode.S))
   {
    direction += Vector3.back;
   }
   if (Input.GetKey(KeyCode.A))
   {
    direction += Vector3.left;
   }
   if (Input.GetKey(KeyCode.D))
   {
    direction += Vector3.right;
   }
   if (Input.GetKey(KeyCode.Q))
   {
    direction += Vector3.down;
   }
   if (Input.GetKey(KeyCode.E))
   {
    direction += Vector3.up;
   }
   return direction;
  }
  
  void Update()
  {
   Vector3 translation = Vector3.zero;
 
#if ENABLE_LEGACY_INPUT_MANAGER
 
   // Exit Sample 按下Esc键退出游戏
   if (Input.GetKey(KeyCode.Escape))
   {
    Application.Quit();
 #if UNITY_EDITOR
 UnityEditor.EditorApplication.isPlaying = false;
 #endif
   }
   // Hide and lock cursor when right mouse button pressed 按下鼠标右键时隐藏并锁定光标
   if (Input.GetMouseButtonDown(1))
   {
    Cursor.lockState = CursorLockMode.Locked;
   }
 
   // Unlock and show cursor when right mouse button released 松开鼠标右键时解锁并显示光标
   if (Input.GetMouseButtonUp(1))
   {
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
   }
 
   // Rotation 旋转
   if (Input.GetMouseButton(1))
   {
    var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));
    
    var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
 
    m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
    m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
   }
   
   // Translation 移动
   translation = GetInputTranslationDirection() * Time.deltaTime;
 
   // Speed up movement when shift key held 按住shift键时加速移动
   if (Input.GetKey(KeyCode.LeftShift))
   {
    //原速度*10为按下Shift后的速度
    translation *= 10.0f;
   }
 
   // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel) 通过增强因子修改移动(在检查器中定义,通过鼠标滚轮在播放模式下修改)
   boost += Input.mouseScrollDelta.y * 0.2f;
   translation *= Mathf.Pow(2.0f, boost);
 
#elif USE_INPUT_SYSTEM
   // TODO: make the new input system work 使新的输入系统正常工作
#endif
 
   m_TargetCameraState.Translate(translation);
 
   // Framerate-independent interpolation 帧率无关插值
   // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time 计算lerp的数量,这样我们就可以在指定的时间内到达目标的99%
   var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
   var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
   m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
 
   m_InterpolatingCameraState.UpdateTransform(transform);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/a71468293a/article/details/109254307

延伸 · 阅读

精彩推荐
  • C#c#中LINQ的基本用法实例

    c#中LINQ的基本用法实例

    语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中引入的一项创新功能。下面这篇文章主要给大家介绍了关于c#中LINQ的基本用法,需要的朋友可以...

    阡陌染6202022-03-09
  • C#Unity实现鼠标或者手指点击模型播放动画

    Unity实现鼠标或者手指点击模型播放动画

    这篇文章主要为大家详细介绍了Unity实现鼠标或者手指点击模型播放动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    liang_7049597215082022-08-17
  • C#一文说通异步 LINQ

    一文说通异步 LINQ

    早期的 LINQ,主要是同步的,直到 C# 8.0 加入 IAsyncEnumerable,LINQ 才真正转向异步。这本来是个非常好的改变,配合 System.Linq.Async 库提供的扩展,可以在诸如...

    老王Plus6612021-09-15
  • C#C#编写的艺术字类实例代码

    C#编写的艺术字类实例代码

    本文给大家分享使用纯C#编写的艺术字类实例代码,代码简单易懂,需要的朋友参考下本教程...

    C#教程网8072021-11-16
  • C#WPF Slider滑动条的颜色修改方法

    WPF Slider滑动条的颜色修改方法

    这篇文章主要介绍了WPF Slider滑动条的颜色修改方法,需要的朋友可以参考下...

    hbatjzyb4312022-02-27
  • C#VS2012 未找到与约束ContractName匹配的导出

    VS2012 未找到与约束ContractName匹配的导出

    这篇文章主要介绍了在更新的windows补丁后,Visual Studio 用户可能无法打开或创建 C++ 或 JavaScript 文件或项目,小编的解决办法,希望可以帮助到大家...

    陈丽娜4722022-02-22
  • C#C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法

    C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法

    这篇文章主要介绍了C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法 ,需要的朋友可以参考下...

    张子浩11062022-07-19
  • C#C#模拟链表数据结构的实例解析

    C#模拟链表数据结构的实例解析

    这篇文章主要介绍了C#模拟链表数据结构的实例解析,包括队双向链表的模拟方法,例子中队链表的操作也有很好的说明,需要的朋友可以参考下...

    灵犀6282021-11-19