Unity3D 小案例技巧02------移动物体+摄像机跟随+鼠标操作摄像机旋转+缩放
编辑器Unity2021
1.创建场地------Plane
2.创建主物体------Capsule
3.删除默认相机
4.创建物体移动脚本----Shift.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shift : MonoBehaviour
{
//物体坐标对象
private Transform m_transform;
//速度
public float m_movSpeed = 10;
void Start()
{
//获取绑定物体的坐标对象
m_transform = GetComponent<Transform>();
}
void Update()
{
Control();
}
void Control() {
//定义3个值控制移动
float xm = 0, ym = 0, zm = 0;
//按键移动 W=上
if (Input.GetKey(KeyCode.W))
{
zm += m_movSpeed * Time.deltaTime;
}
else if(Input.GetKey(KeyCode.S))// S=下
{
zm -= m_movSpeed * Time.deltaTime;
}
//按键移动 A=左
if (Input.GetKey(KeyCode.A))
{
xm -= m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))// D=右
{
xm += m_movSpeed * Time.deltaTime;
}
//跳 = C
if (Input.GetKey(KeyCode.C))
{
ym += m_movSpeed * Time.deltaTime;
}
//落 = X
if (Input.GetKey(KeyCode.X) && m_transform.position.y >= 1)
{
ym -= m_movSpeed * Time.deltaTime;
}
//更新物体坐标位置
m_transform.Translate(new Vector3(xm,ym,zm),Space.Self);
}
}
5.将脚本拖入主物体下

6.创建空组件4个,再在最下面创建一个摄像机Camera

7.创建脚本-----CameraShift.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShift : MonoBehaviour
{
//控制左右旋转
public Transform y_Axis;
//控制上下旋转
public Transform x_Axis;
//控制左右倾斜
public Transform z_Axis;
//控制远近距离
public Transform zoom_Axis;
//物体对象
public Transform player;
//旋转速度
public float roSpeed = 180;
//缩放速度
public float scSpeed = 50;
//限定角度
public float limitAngle = 45;
//鼠标左右滑动数值、滚动数值
private float hor, ver, scrollView;
float x = 0,sc = 10;
//是否跟随物体对象
public bool followFlag;
//是否控制物体旋转
public bool turnFlag;
private void LateUpdate()
{
//输入获取
hor = Input.GetAxis("Mouse X");
ver = Input.GetAxis("Mouse Y");
//鼠标滚动数值
scrollView = Input.GetAxis("Mouse ScrollWheel");
//左右滑动鼠标
if (hor!=0)
{
//围绕Y轴旋转,注意Up是本地坐标的位置
y_Axis.Rotate(Vector3.up*roSpeed*hor*Time.deltaTime);
}
//上下滑动鼠标
if (ver!=0)
{
//Y轴移动值
x += -ver * Time.deltaTime * roSpeed;
//设置鼠标移动范围
x = Mathf.Clamp(x, -limitAngle,limitAngle);
//Quaternion.identity:单位旋转
Quaternion q = Quaternion.identity;
//Quaternion.Euler:返回一个旋转,它围绕 z 轴旋转 z 度、围绕 x 轴旋转 x 度、围绕 y 轴旋转 y 度
q = Quaternion.Euler(new Vector3(x,x_Axis.eulerAngles.y,x_Axis.eulerAngles.z));
//Quaternion.Lerp:在 a 和 b 之间插入 t,然后对结果进行标准化处理。参数 t 被限制在 [0, 1] 范围内。
//将旋转值赋值给摄像机坐标对象
x_Axis.rotation = Quaternion.Lerp(x_Axis.rotation,q, Time.deltaTime*roSpeed);
}
//缩放远近
if (scrollView!=0)
{
//远近移动值
sc -= scrollView * scSpeed;
//设置鼠标移动范围
sc = Mathf.Clamp(sc,3,10);
//将缩放值赋值给控制远近距离对象
zoom_Axis.transform.localPosition = new Vector3(0,0,-sc);
}
//跟随物体对象 当指定物体后
if (followFlag&&player!=null)
{
//Vector3.Lerp:在两个点之间进行线性插值。
//Y轴方向的坐标同步
y_Axis.position = Vector3.Lerp(y_Axis.position,player.position+Vector3.up, Time.deltaTime * 10f);
}
//旋转物体对象 当指定物体后
if (followFlag && player != null)
{
//旋转值同步
player.transform.forward = new Vector3(transform.forward.x,0,transform.forward.z);
}
}
}
8.将脚本添加到摄像机组件下

9.将Y X Z Zoom 主物体(Capsule)拖入对应框中
10.勾选跟随和同步


浙公网安备 33010602011771号