我是靠谱客的博主 凶狠服饰,这篇文章主要介绍Unity3D实现鼠标控制视角转动,现在分享给大家,希望可以做个参考。

前面,学了物体的移动功能,现在来学一下C#实现鼠标控制摄像机(视角)移动。

代码如下:

C#脚本(在Unity 5.5.1 下能运行):

复制代码
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
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MouseView : MonoBehaviour { public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes m_axes = RotationAxes.MouseXAndY; public float m_sensitivityX = 10f; public float m_sensitivityY = 10f; // 水平方向的 镜头转向 public float m_minimumX = -360f; public float m_maximumX = 360f; // 垂直方向的 镜头转向 (这里给个限度 最大仰角为45°) public float m_minimumY = -45f; public float m_maximumY = 45f; float m_rotationY = 0f; // Use this for initialization void Start () { // 防止 刚体影响 镜头旋转 if (GetComponent<Rigidbody>()) { GetComponent<Rigidbody> ().freezeRotation = true; } } // Update is called once per frame void Update () { if (m_axes == RotationAxes.MouseXAndY) { float m_rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * m_sensitivityX; m_rotationY += Input.GetAxis ("Mouse Y") * m_sensitivityY; m_rotationY = Mathf.Clamp (m_rotationY, m_minimumY, m_maximumY); transform.localEulerAngles = new Vector3 (-m_rotationY, m_rotationX, 0); } else if (m_axes == RotationAxes.MouseX) { transform.Rotate (0, Input.GetAxis ("Mouse X") * m_sensitivityX, 0); } else { m_rotationY += Input.GetAxis ("Mouse Y") * m_sensitivityY; m_rotationY = Mathf.Clamp (m_rotationY, m_minimumY, m_maximumY); transform.localEulerAngles = new Vector3 (-m_rotationY, transform.localEulerAngles.y, 0); } } }

调用时,只需把该脚本绑定给物体即可。这里是绑定摄像机,以摄像机为第一人称视角转动。

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

最后

以上就是凶狠服饰最近收集整理的关于Unity3D实现鼠标控制视角转动的全部内容,更多相关Unity3D实现鼠标控制视角转动内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(302)

评论列表共有 0 条评论

立即
投稿
返回
顶部