固定相机跟随

这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动

using UnityEngine;
using System.Collections; public class CameraFlow : MonoBehaviour
{
public Transform target;
private Vector3 offset;
// Use this for initialization
void Start()
{
offset = target.position - this.transform.position; } // Update is called once per frame
void Update()
{
this.transform.position = target.position - offset;
}
}

固定相机跟随,带有角度旋转

这一种相机跟随是对第一种相机跟随的改进,在原有基础上面,添加了跟随角度的控制

using UnityEngine;
using System.Collections; public class CameriaTrack : MonoBehaviour {
private Vector3 offset = new Vector3(,,);//相机相对于玩家的位置
private Transform target;
private Vector3 pos; public float speed = ; // Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame
void Update () {
pos = target.position + offset;
this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//调整相机与玩家之间的距离
Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//获取旋转角度
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime); }
}

第三人称相机

这种相机跟随,是第三人称角度看向对象的,也就是一直看向对象的后面,如一直显示玩家的后背

using UnityEngine;
using System.Collections;
//相机一直拍摄主角的后背
public class CameraFlow : MonoBehaviour { public Transform target; public float distanceUp=15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移动值
public float camDepthSmooth = 5f;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
// 鼠标轴控制相机的远近
if ((Input.mouseScrollDelta.y < && Camera.main.fieldOfView >= ) || Input.mouseScrollDelta.y > && Camera.main.fieldOfView <= )
{
Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
} } void LateUpdate()
{
//相机的位置
Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
//相机的角度
transform.LookAt(target.position);
} }

相机跟随,鼠标控制移动和缩放

相机与观察对象保持一定距离,可以通过鼠标进行上下左右旋转,通过鼠标滚轮进行放大和缩小操作

using UnityEngine;
using System.Collections; public class CameraFlow : 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") * ;
Debug.Log("dis=" + dis);
if (dis < || dis > )
{
return;
}
offset = offset.normalized * dis;
}
//左右上下移动
private void Rotate()
{
if (Input.GetMouseButton())
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles; //围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * );
transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * );
float x = transform.eulerAngles.x;
float y = transform.eulerAngles.y;
Debug.Log("x=" + x);
Debug.Log("y=" + y);
//控制移动范围
if (x < || x > || y < || y > )
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相对差值
offset = transform.position - target.position;
} }
}

Unity相机跟随的更多相关文章

  1. unity相机跟随Player常用方式

    固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...

  2. Unity相机跟随-----根据速度设置偏移量

    这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感. ...

  3. Unity中几种简单的相机跟随

    #unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...

  4. Unity相机平滑跟随

    简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...

  5. unity 常用的几种相机跟随

    固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...

  6. unity3D:游戏分解之角色移动和相机跟随

          游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...

  7. unity3d简单的相机跟随及视野旋转缩放

    1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...

  8. SurvivalShooter学习笔记(一.相机跟随)

    1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...

  9. unity_实用小技巧(相机跟随两个主角移动)

    在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...

随机推荐

  1. 数位dp(模板+例题)

    文章参考:数位dp之总结 首先,什么是数位dp?它是干什么的? 数位dp是一种计数用的dp,一般就是要统计一个区间[le,ri]内满足一些条件数的个数. 举个栗子: 加入我们要枚举所有上界不超过231 ...

  2. linux基础之CnetOS安装

    CentOS启动流程 POST-->boot sequence(bios)--> bootloader(mbr)-->kernel(ramdisk)-->rootfs(ro)- ...

  3. Longest Ordered Subsequence POJ - 2533 dp 最长上升/不下降 子序列

    #include<iostream> using namespace std ; ; int f[N]; int a[N]; int n; int main() { cin>> ...

  4. If no other git process is currently running, this probably means a git proc

    原因:用SourceTree提交代码,发现这个问题.好像是因为上个进程没停止,造成文件不识别 解决:把仓库目录里的.git/index.lock文件(文件是隐藏的)删除就可以了.删除index.loc ...

  5. 如何将.sql文件导入到mysql的数据库中

    首先通过cmd的net start mysql57 启动mysql的服务器 然后,输入命令:mysql -h 127.0.0.1 -u root -p来启动mysql服务 最后 上图画红圈的部分是.s ...

  6. 【你不知道的javaScript 上卷 笔记3】javaScript中的声明提升表现

    console.log( a ); var a = 2; 执行输出undefined a = 2; var a; console.log( a ); 执行输出2 说明:javaScript 运行时在编 ...

  7. Python之路Day11

    函数名的第一类对象及使用 当作值,赋值给变量 def func(): print(1) print(func) #查看函数的内存地址 a=func print(a) a() 可以当作容器中的元素 de ...

  8. Ubuntu中chrome浏览器安装、卸载

    一.卸载 sudo apt-get autoremove google-chrome-stable 删除下载源:sudo rm /etc/apt/sources.list.d/google-chrom ...

  9. PP: Shape and time distortion loss for training deep time series forecasting models

    Problem: time series forecasting Challenge: forecasting for non-stationary signals and multiple futu ...

  10. IIS添加网站

    打开IIS 在网站上面点击右键进行添加网站 进入添加网站配置