我是靠谱客的博主 可爱人生,最近开发中收集的这篇文章主要介绍unity通过鼠标操作摄像机围绕主角缩放、旋转、上下移动,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

控制摄像机的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 摄像机控制器.
将该脚本挂载到CameraFollowAndRotate空物体身上.
/// </summary>
public class CameraController : MonoBehaviour
{
public static CameraController instance;
/// <summary>
/// 控制摄像机的上下.
/// </summary>
[SerializeField]
private Transform _CameraUpAndDown;
/// <summary>
/// 控制摄像机缩放.
/// </summary>
[SerializeField]
private Transform _CameraZoomContainer;
/// <summary>
/// 摄像机容器.
/// </summary>
[SerializeField]
private Transform _CameraContainer;
void Awake()
{
instance = this;
}
/// <summary>
/// 设置摄像机旋转.
/// </summary>
/// <param name="type">0代表左
1代表右</param>
public void SetCameraRotate(int type)
{
transform.Rotate(new Vector3(0,20*Time.deltaTime*(type==1?-1:1),0));
}
/// <summary>
/// 设置摄像机上下.
/// </summary>
/// <param name="type">0代表上
1代表下</param>
public void SetCameraUpAndDown(int type)
{
_CameraUpAndDown.Rotate(20 * Time.deltaTime * (type == 0 ? -1 : 1),0,0);
//限定摄像机上下.
注意事项:不要把_CameraUpAndDown这个空物体的旋转最小值设置成负数
_CameraUpAndDown.localEulerAngles = new Vector3(Mathf.Clamp(_CameraUpAndDown.localEulerAngles.x,5,70),0,0);
}
/// <summary>
/// 设置摄像机拉近拉远.
/// </summary>
public void SetCameraZoom(float value)
{
_CameraContainer.Translate(Vector3.forward * value * Time.deltaTime * 1000);
//限定摄像机最远和最近距离.
_CameraContainer.localPosition = new Vector3(0,0,Mathf.Clamp(_CameraContainer.localPosition.z,-26,26));
}
/// <summary>
/// 绘制摄像机运动轨迹.
/// </summary>
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position,24);
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, 19);
}
}

 鼠标滑动拖拽事件代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 拖拽事件脚本.
将该脚本挂载到Panel身上.
/// </summary>
public class DragEvent : MonoBehaviour, IDragHandler,IPointerDownHandler
{
private Vector2 oldMousePos;//记录上一次鼠标点中屏幕的坐标.
public static DragEvent instance;
void Awake()
{
instance = this;
}
/// <summary>
/// 拖拽的方向.
/// </summary>
public enum DragDir
{
Left,
Right,
Up,
Down
}
/// <summary>
/// 缩放类型.
/// </summary>
public enum ZoomType
{
In,//向内.
Out//向外.
}
/// <summary>
/// 拖拽事件
/// </summary>
public Action<DragDir> onDragEvent;
public Action<ZoomType,float> onZoomEvent;
void Update()
{
//获取鼠标滚轮键滑动的值.
>0代表拉近
<0代表拉远.
if (Input.GetAxis("Mouse ScrollWheel")>0)
{
if (onZoomEvent != null)
{
onZoomEvent(ZoomType.In, Input.GetAxis("Mouse ScrollWheel"));
}
}else if (Input.GetAxis("Mouse ScrollWheel")< 0)
{
if (onZoomEvent != null)
{
onZoomEvent(ZoomType.Out, Input.GetAxis("Mouse ScrollWheel"));
}
}
}
/// <summary>
/// 拖拽进行中.
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
Vector2 dir = eventData.position - oldMousePos;
//计算鼠标滑动的方向.
if(dir.y<dir.x&& dir.y > -dir.x)
{
if (onDragEvent != null)
{
onDragEvent(DragDir.Right);
}
}else if(dir.y>dir.x && dir.y < -dir.x)
{
if (onDragEvent != null)
{
onDragEvent(DragDir.Left);
}
}
else if (dir.y > dir.x && dir.y>-dir.x)
{
if (onDragEvent != null)
{
onDragEvent(DragDir.Up);
}
}
else
{
if (onDragEvent != null)
{
onDragEvent(DragDir.Down);
}
}
}
/// <summary>
/// 鼠标在屏幕按下的时候.
/// </summary>
public void OnPointerDown(PointerEventData eventData)
{
oldMousePos = eventData.position;
}
}

控制角色的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 角色控制器.
将该脚本挂载到Player游戏物体身上.
/// </summary>
public class PlayerController : MonoBehaviour {
private Transform _Transform;
void Start()
{
_Transform = gameObject.GetComponent<Transform>();
//订阅事件.
DragEvent.instance.onDragEvent += OnDragEvent;
DragEvent.instance.onZoomEvent += OnZoomEvent;
}
void OnDestroy()
{
//取消事件.
DragEvent.instance.onDragEvent -= OnDragEvent;
DragEvent.instance.onZoomEvent -= OnZoomEvent;
}
/// <summary>
/// 事件回调.
/// </summary>
private void OnDragEvent(DragEvent.DragDir dir)
{
switch (dir)
{
case DragEvent.DragDir.Left:
CameraController.instance.SetCameraRotate(1);
break;
case DragEvent.DragDir.Right:
CameraController.instance.SetCameraRotate(0);
break;
case DragEvent.DragDir.Up:
CameraController.instance.SetCameraUpAndDown(0);
break;
case DragEvent.DragDir.Down:
CameraController.instance.SetCameraUpAndDown(1);
break;
}
}
/// <summary>
/// 摄像机缩放事件回调.
/// </summary>
/// <param name="zoom"></param>
/// <param name="value"></param>
private void OnZoomEvent(DragEvent.ZoomType zoom,float value)
{
switch (zoom)
{
case DragEvent.ZoomType.In:
CameraController.instance.SetCameraZoom(value);
break;
case DragEvent.ZoomType.Out:
CameraController.instance.SetCameraZoom(value);
break;
}
}
void Update()
{
CameraAutoFollow();
}
/// <summary>
/// 摄像机自动跟随.
/// </summary>
private void CameraAutoFollow()
{
if (CameraController.instance != null)
{
CameraController.instance.transform.position = _Transform.position;
}
}
}

 

最后

以上就是可爱人生为你收集整理的unity通过鼠标操作摄像机围绕主角缩放、旋转、上下移动的全部内容,希望文章能够帮你解决unity通过鼠标操作摄像机围绕主角缩放、旋转、上下移动所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部