http://blog.csdn.net/begonia__z/article/details/51178907
前天撸了一个简单的UGUI虚拟摇杆,今天我就利用前天做的虚拟摇杆做了一个简单的摄像机控制器,主要看看UGUI虚拟摇杆是否可以完美的控制移动和旋转。(PS:主要是为接下来的项目做技术测试),手游版的CF的角色控制器也是利用设个方式进行摄像机的控制,利用双摇杆进行角色的前进和旋转,如果你玩过PSV或者是用过手柄控制器就知道啥意思了,接下来我们就来实现这个功能需求。
首先我们先做两个简单的虚拟摇杆,如下图所示:
因为我们是要实现摄像机的移动,因此我就直接在MainCamera上添加脚本,手游CF左边虚拟摇杆是用来控制角色移动,右边是控制角色的射击以及角色的旋转,那么我们现在我们来实现个功能。因为功能是否简单所以我就直接上代码了。代码如下所示:
- using System;
- using UnityEngine;
- using System.Collections;
- public class ControlCamera : MonoBehaviour
- {
- public float moveSpeed = 100.0f;
- public float rotateSpeed = 1;
- public float minAngleY = -30f;
- public float maxAngleY = 30f;
- public ScrollCircle joyStick1;
- public ScrollCircle joyStick2;
- private Vector2 angleVector2 = Vector2.zero;
- // Update is called once per frame
- void Update ()
- {
- UpdateCamera();
- }
- /// <summary>
- /// 更新摄像机位置以及旋转角度
- /// </summary>
- public void UpdateCamera()
- {
- if (!joyStick1.GetIsOnEndDrag() && joyStick1.GetOffsetVector3() != Vector3.zero)
- {
- this.transform.localPosition = new Vector3(
- this.transform.localPosition.x + (joyStick1.GetOffsetVector3().x*moveSpeed),
- this.transform.localPosition.y,
- this.transform.localPosition.z + (joyStick1.GetOffsetVector3().y*moveSpeed));
- }
- if (!joyStick2.GetIsOnEndDrag() & joyStick2.GetOffsetVector3() != Vector3.zero)
- {
- angleVector2.x += joyStick2.GetOffsetVector3().x*rotateSpeed;
- angleVector2.y -= joyStick2.GetOffsetVector3().y*rotateSpeed;
- angleVector2.y = ClampAngle(angleVector2.y, minAngleY, maxAngleY);
- Quaternion rotateAngle = Quaternion.Euler(angleVector2.y, angleVector2.x, 0);
- this.transform.localRotation = rotateAngle;
- angleVector2.y = ClampAngle(angleVector2.y, minAngleY, maxAngleY);
- }
- }
- /// <summary>
- /// 限制选择角度
- /// </summary>
- /// <param name="angle"></param>
- /// <param name="min"></param>
- /// <param name="max"></param>
- /// <returns></returns>
- 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);
- }
- }
因为代码很简单所以我只是备注了一下函数用法,如果有不懂可以私聊我。
代码可能写的不好,欢迎大神们指导教育0.0
2016/4/18 15:46
刚刚发现有仔细看代码的时候发现一个严重bug,Y轴旋转180度后你会发现所有的移动都反了,这样影响到了操作了因此我们需要把这个严重的bug修复了,这里我只把UpdateCamera函数放出来,修改如下:
- public void UpdateCamera()
- {
- if (!joyStick1.GetIsOnEndDrag() && joyStick1.GetOffsetVector3() != Vector3.zero)
- {
- movePos.x = joyStick1.GetOffsetVector3().x*moveSpeed;
- movePos.z = joyStick1.GetOffsetVector3().y*moveSpeed;
- float y = transform.localRotation.eulerAngles.y;
- movePos = Quaternion.Euler(0, y, 0)*movePos;
- Vector3 currentPoint = new Vector3(transform.localPosition.x + movePos.x, transform.localPosition.y,
- transform.localPosition.z + movePos.z);
- this.transform.localPosition = currentPoint;
- }
- if (!joyStick2.GetIsOnEndDrag() & joyStick2.GetOffsetVector3() != Vector3.zero)
- {
- angleVector2.x += joyStick2.GetOffsetVector3().x*rotateSpeed;
- angleVector2.y -= joyStick2.GetOffsetVector3().y*rotateSpeed;
- angleVector2.y = ClampAngle(angleVector2.y, minAngleY, maxAngleY);
- Quaternion rotateAngle = Quaternion.Euler(angleVector2.y, angleVector2.x, 0);
- this.transform.localRotation = rotateAngle;
- angleVector2.y = ClampAngle(angleVector2.y, minAngleY, maxAngleY);
- }
- }