Unity3D如何实现控制摄像机移动

发布时间:2021-05-24 14:02:29 作者:小新
来源:亿速云 阅读:1964

这篇文章将为大家详细讲解有关Unity3D如何实现控制摄像机移动,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

最近公司的几个项目开发内容基本相同,很多脚本直接复制过来就可以拼接项目。之前一直是代码爱好者,能自己敲的绝对不去复制粘贴。但是开发速度确实是被耽误了,所以接下来打算把开发中常用的脚本都发到博客上。自己需要的时候直接拿来。也希望能帮到你们。

unity编辑器中按住鼠标右键,在通过控制键盘的wasdqe键可以自由控制视野。

下面就是实现操作的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
//巡游模式摄像机控制
public class CameraMove : MonoBehaviour
{
 
 public static CameraMove Instance = null;
 
 private Vector3 dirVector3;
 private Vector3 rotaVector3;
 private float paramater = 0.1f;
 //旋转参数
 private float xspeed = -0.05f;
 private float yspeed = 0.1f;
 
 private float dis;
 
 void Awake ()
 {
 Instance = this;
 }
 
 private void Start()
 {
 rotaVector3 = transform.localEulerAngles;
 dis = UIFuc.Instance.Dis;
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
 //旋转
 if (Input.GetMouseButton(1))
 {
  rotaVector3.y += Input.GetAxis("Horizontal") * yspeed;
  rotaVector3.x += Input.GetAxis("Vertical") * xspeed;
  transform.rotation = Quaternion.Euler(rotaVector3);
 }
 
 //移动
 dirVector3 =Vector3.zero;
 
 if (Input.GetKey(KeyCode.W))
 {
  if(Input.GetKey(KeyCode.LeftShift)) dirVector3.z = 3;
  else dirVector3.z = 1;
 }
 if (Input.GetKey(KeyCode.S))
 {
  if (Input.GetKey(KeyCode.LeftShift)) dirVector3.z = -3;
  else dirVector3.z = -1;
 }
 if (Input.GetKey(KeyCode.A))
 {
  if (Input.GetKey(KeyCode.LeftShift)) dirVector3.x = -3;
  else dirVector3.x = -1;
 }
 if (Input.GetKey(KeyCode.D))
 {
  if (Input.GetKey(KeyCode.LeftShift)) dirVector3.x = 3;
  else dirVector3.x = 1;
 }
 if (Input.GetKey(KeyCode.Q))
 {
  if (Input.GetKey(KeyCode.LeftShift)) dirVector3.y = -3;
  else dirVector3.y = -1;
 }
 if (Input.GetKey(KeyCode.E))
 {
  if (Input.GetKey(KeyCode.LeftShift)) dirVector3.y = 3;
  else dirVector3.y = 1;
 }
 transform.Translate(dirVector3 * paramater,Space.Self);
 //限制摄像机范围
 transform.position = Vector3.ClampMagnitude(transform.position, dis);
 }
}

由于项目需要限制摄像机的移动范围,所以我在最后加上了限制的代码。

关于“Unity3D如何实现控制摄像机移动”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. unity3d实现控制摄像机放大缩小视野加左右可旋转视角角度
  2. [Unity3d]多个摄像机叠加效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity

上一篇:Unity3D如何实现鼠标控制旋转转盘

下一篇:Unity3D如何实现人物转向与移动

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》