Unity相机跟随
固定相机跟随
这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动
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相机跟随的更多相关文章
- unity相机跟随Player常用方式
固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...
- Unity相机跟随-----根据速度设置偏移量
这里假设在水中的船,船有惯性,在不添加前进动力的情况下会继续移动,但是船身是可以360度自由旋转,当船的运动速度在船的前方的时候,相机会根据向前的速度的大小,设置相机的偏移量,从而提高游戏的动态带感. ...
- Unity中几种简单的相机跟随
#unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...
- Unity相机平滑跟随
简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- unity3D:游戏分解之角色移动和相机跟随
游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...
- unity3d简单的相机跟随及视野旋转缩放
1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...
- SurvivalShooter学习笔记(一.相机跟随)
1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...
- unity_实用小技巧(相机跟随两个主角移动)
在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...
随机推荐
- python3中的参数*args
python的传参是如何实现的 # 将未拆包的数据进行传参 def run(a,*args): #第一个参数传给了a print(a) # args是一个元组,里面是2和3两个参数 print(a ...
- Linux shell unzip和tar 批量解压文件
#!/bin/sh UNTAR="tar -xvf " #unzip all zip files function unzip_all_file() { for i in *.zi ...
- Oracle 12c 如何在 PDB 中添加 SCOTT 模式(手工方式)
Oracle 12c 建库后,没有 scott 模式,本篇使用手工脚本方式,在12c版本中创建 scott 模式及相关表. 目录 1. PDB中创建用户 2. PDB中用户授权 3. PDB中创建表空 ...
- 变量 Variables
是为了存储(store)程序(program)运算过程中的一些信息(informations),为了方便日后调用.操作和更改 变量名应该简明,见名识意,让读者和我们自己能更清晰的了解 如果我们把变量看 ...
- Nginx proxy_cache 缓存静态文件
原文链接:https://blog.csdn.net/bjgaocp/article/details/87867521 创建缓存目录mkdir /tmp/ngx_cache 添加下面语句在http{ ...
- python数据分析学习(2)pandas二维工具DataFrame讲解
目录 二:pandas数据结构介绍 下面继续讲解pandas的第二个工具DataFrame. 二:pandas数据结构介绍 2.DataFarme DataFarme表示的是矩阵的数据表,包含 ...
- python开发第四篇:函数(1)
函数 阅读目录 一 函数知识体系 二 函数基础 三 函数对象.函数嵌套.名称空间与作用域.装饰器 四 迭代器.生成器.面向过程编程 五 三元表达式.列表推导式.生成器表达式.递归.匿名函数.内置函数 ...
- beego——view 模板语法
一.基本语法 go统一使用{{和}}作为左右标签,没有其它的标签符号. 使用"."来访问当前位置的上下文,使用"$"来引用当前模板根级的上下文,使用$var来访 ...
- 曼孚科技:AI领域9种常见的监督学习算法
监督学习是机器学习中一种十分重要的算法.与无监督学习相比,监督学习有明确的目标. 分类与回归是监督学习两个主要任务,常见的监督学习算法主要有以下9种: 1 朴素贝叶斯 分类 2 决策树 分类 3 支 ...
- C++-hihoCoder-1142-三分求极值[三分]
#include <bits/stdc++.h> using namespace std; typedef double db; ; db a,b,c,x,y; db dis2(db X) ...