Unity第一人称和第三人称视角脚本
2015-07-29 15:02
661 查看
Unity第一人称和第三人称视角
第一人称视角
public class FirstView : MonoBehaviour { //要相机跟随的GameObject public Transform m_target; //鼠标敏度 public float mousesSensity = 10F; //上下最大视角(Y视角) public float minYLimit = -20F; public float maxYLimit = 80F; Vector3 m_camRotation; void Update() { //根据鼠标的移动,获取相机旋转的角度 m_camRotation.x = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousesSensity; m_camRotation.y += Input.GetAxis("Mouse Y") * mousesSensity; //角度限制 m_camRotation.y = Mathf.Clamp(m_camRotation.y, minYLimit, maxYLimit); //相机角度随着鼠标旋转 transform.localEulerAngles = new Vector3(-m_camRotation.y, m_camRotation.x, 0); transform.position = m_target.position; } void Start() { // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; } }
使用方法:摄像机添加改脚本
target:要相机跟随的GameObject(主角Player)
minYLimit方向最小视角
minXLimit方向最大视角
mousesSensity:水平方向和垂直方向鼠标移动速度
第三人称视角
using UnityEngine; using System.Collections; public class ThirdView : MonoBehaviour { public Transform target; public float distance = 10.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float yMinLimit = -20.0f; public float yMaxLimit = 80.0f; private float x = 0.0f; private float y = 0.0f; // Use this for initialization void Start () { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; } void LateUpdate () { if (target) { x += Time.deltaTime * Input.GetAxis("Mouse X") * xSpeed; y -= Time.deltaTime * Input.GetAxis("Mouse Y") * ySpeed; y = ClampAngle(y, yMinLimit, yMaxLimit); // var rotation = Quaternion.EulerAngles(y * Mathf.Deg2Rad, x * Mathf.Deg2Rad, 0); var rotation=Quaternion.Euler(y,x,0); Vector3 vec = new Vector3(0.0f, 0.0f, -distance); var position = rotation * vec + target.position; transform.rotation = rotation; transform.position = position; } } 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); } // Update is called once per frame void Update () { } }
使用方法:摄像机添加改脚本
target:要相机跟随的GameObject(主角Player)
distance:相机距离Tatget的距离(远近镜头调整)
yMinLimit:Y方向最小视角
yMaxLimit:Y方向最大视角
xSpeed:水平方向鼠标移动速度
ySpeed:垂直方向鼠标移动速度
相关文章推荐
- 基于Unity5.0系统的新打包方式
- DoTween(HOTween V2) 教程
- VS2013无法调试Unity3D问题
- 【Unity】代码加密(二)dll加密so加密
- Unity5中Inspector界面上的AssetBundle值设定问题
- [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引
- [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引
- Unity协程(Coroutine)原理深入剖析
- Unity3d 对特效进行缩放
- 四元数与欧拉角(yaw roll pitch)转换公式与推导,以及它们unity中的不同
- [转载]unity优化1
- 非常详细的测试unity与android之间的通讯操作
- [Unity3D]Unity3D叙利亚NGUI血液和技能的冷却效果
- 免费的Visual Studio Community 2015适用于你么?
- 噩梦 游戏结束 检测血量执行动画
- Unity3d NGUI如何支持中文
- Unity C# 规范
- Unity查找脚本被哪些Perfab或场景引用
- 09应用输入经理旋转场景--《猿学校课程Unity3d》
- Unity协程(Coroutine)原理深入剖析再续