📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📢本文由 God_Y原创🙉
📢未来很长,值得我们全力奔赴更美好的生活✨
相机视角跟随 的几种方式👻
因为平时无论是做游戏,还是写写小Demo,让相机跟随物体移动进行视角控制还是用的挺多的 下面就来介绍几种可以控制摄像机视角跟随角色物体移动的几种方式。 (忽略我随意搭建的测试场景,low的很...哈哈)
这是简单控制物体移动的脚本,挂到物体上移动测试用的 写的很简单,通过按键盘上的上下左右就可以进行移动和转身
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerTest1 : MonoBehaviour
{
[Header("移动速度")]
public float movespeed = 5f;
[Header("转身速度")]
public float turnspeed = 5f;
private float hor;
private float ver;
void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
//前后移动
transform.position += ver * transform.forward * Time.deltaTime * movespeed;
//左右转身
transform.eulerAngles += hor * Vector3.up * turnspeed;
}
}
复制代码
第三人称视角相机跟随🎃
因为控制视角跟随的方法有很多种,在这里就只介绍三种方法,功能各有所不同,不过区别不是很大,都是第三人称视角来的~~ 以下都是通过在Unity界面直接将要追踪的物体拖到摄像机的脚本上的
①==普通摄像机视角跟随==🎅 直接将场景中的Camera拖到该物体上![在这里插入图片描述](https://res.hc-cdn.com/ecology/9.3.104/v2_resources/ydcomm/libs/images/loading.gif)
这是最简便的方法,也不用写代码,将摄像机挂上去即可。 优点:操作简单,都不用写代码的 缺点:旋转的时候效果不好,头晕目眩~
②==普通摄像机视角跟随==🎄 声明一个方向向量,计算摄像机指向玩家的方向偏移量,从而计算出出摄像机的位置,然后让摄像机一直随着这个数值即可。 脚本挂载到摄像机上即可![在这里插入图片描述](https://res.hc-cdn.com/ecology/9.3.104/v2_resources/ydcomm/libs/images/loading.gif)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraTest : MonoBehaviour
{
//跟随的目标
public Transform target;
//方向向量
private Vector3 dir;
private void Start()
{
//计算摄像机指向玩家的方向偏移量
dir = target.position - transform.position;
}
private void Update()
{
//时时刻刻计算摄像机的跟随位置
Vector3 bastPos = target.position - dir;
transform.position = bastPos;
}
}
复制代码
③==可以通过鼠标滑轮控制视角缩放的跟随==🎁 这个的视角跟随效果与第二个类似,多了一个通过鼠标滑轮控制视角缩放的效果 代码![在这里插入图片描述](https://res.hc-cdn.com/ecology/9.3.104/v2_resources/ydcomm/libs/images/loading.gif)
using UnityEngine;
using System.Collections;
public class camera : MonoBehaviour
{
public Transform target;
Vector3 offset;
// Use this for initialization
void Start()
{
offset = transform.position - target.position;
}
// Update is called once per frame
void Update()
{
transform.position = target.position + offset;
Rotate();
Scale();
}
//缩放
private void Scale()
{
float dis = offset.magnitude;
dis -= Input.GetAxis("Mouse ScrollWheel") * 5;
Debug.Log("dis=" + dis);
if (dis < 10 || dis > 40)
{
return;
}
offset = offset.normalized * dis;
}
//左右上下移动
private void Rotate()
{
if (Input.GetMouseButton(1))
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles;
//围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
float x = transform.eulerAngles.x;
float y = transform.eulerAngles.y;
Debug.Log("x=" + x);
Debug.Log("y=" + y);
//控制移动范围
if (x < 20 || x > 45 || y < 0 || y > 40)
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相对差值
offset = transform.position - target.position;
}
}
}
复制代码
④==相机一直拍摄角色的后背的视角==🔔 这个相机跟随是一直拍角色后背的视角,会自动调整![在这里插入图片描述](https://res.hc-cdn.com/ecology/9.3.104/v2_resources/ydcomm/libs/images/loading.gif)
using UnityEngine;
using System.Collections;
//相机一直拍摄主角的后背
public class cameraT4 : MonoBehaviour
{
public Transform target;
public float distanceUp = 15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移动值
public float camDepthSmooth = 5f;
void Update()
{
// 鼠标轴控制相机的远近
if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView |