unity3d之相机跟随人物
一.第三人称视角 _1
先设置好相机与玩家之间的角度

给相机添加代码
using UnityEngine;
using System.Collections; namespace CompleteProject
{
public class CameraFollow : MonoBehaviour
{
public Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following. Vector3 offset; // The initial offset from the target. void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
} void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset; // Smoothly interpolate between the camera's current position and it's target position.
//相机平滑的移动到目标位置,插值
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
}
二、第三人称视角_2,可自己根据游戏效果调节位置
在inspector面板修改mHeight与mDistance值即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class FollowCamera : MonoBehaviour
{
public Transform mTarget; //相机的跟随目标
public float mHeight=8f;
public float mDistance=6f;
public float interpolation = 20.0f;
public Space space = Space.World; // Use this for initialization
void Start ()
{ } // Update is called once per frame
void LateUpdate ()
{
if (mTarget)
{
Vector3 dest = Vector3.zero;
switch (space)
{
case Space.World://以世界为中心
dest = mTarget.position + Vector3.up * mHeight - Vector3.forward * mDistance;
break;
case Space.Self://玩家为中心,一般用不到可省略switch
dest = mTarget.position + mTarget.up * mHeight - mTarget.forward * mDistance;
break;
default:
break;
}
transform.position = Vector3.Lerp(transform.position, dest, interpolation);
transform.LookAt(mTarget.position);
}
}
}
unity3d之相机跟随人物的更多相关文章
- unity相机跟随Player常用方式
固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...
- Unity3d学习 相机的跟随
最近在写关于相机跟随的逻辑,其实最早接触相机跟随是在Unity官网的一个叫Roll-a-ball tutorial上,其中简单的涉及了关于相机如何跟随物体的移动而移动,如下代码: using Unit ...
- unity3d简单的相机跟随及视野旋转缩放
1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...
- unity3D:游戏分解之角色移动和相机跟随
游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...
- SurvivalShooter学习笔记(一.相机跟随)
1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- Unity中几种简单的相机跟随
#unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...
- unity_实用小技巧(相机跟随两个主角移动)
在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...
- Unity相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
随机推荐
- P4850 [IOI2009]葡萄干raisins 记忆化搜索
$ \color{#0066ff}{ 题目描述 }$ 普罗夫迪夫的著名巧克力大师Bonny需要切开一板带有葡萄干的巧克力.巧克力是一个包含许多相同的方形小块的矩形.小块沿着巧克力的边排列成n行m列,即 ...
- 斐讯K2P通过配置文件开启telnet的原理分析
看过几篇教程之后我已经知道怎么备份固件了.但是现在有一个问题,我的本意是把K2P原机带的固件备份出来,用教程上的方法进行开启telnet.备份固件等操作是否会改变固件呢?下面我们来验证这个问题. Op ...
- nginx下重写隐藏index.php文件
location / { root /项目目录/; index index.php; if (-f $request_filename/index.php){ rewrite (.*) $1/inde ...
- 使用私有git仓库备份服务器脚本和配置文件
1. 创建私有git仓库 服务器端配置: # 安装 git yum -y install git # 创建 git 用户 useradd git # 创建私有仓库数据存储目录 mkdir /git_b ...
- PHP打开错误提示和关闭错误提示的方法
找到php的配置文件,也就是php.ini 在文件中查找 ‘display_errors’ 查找到 display_errors = Off 或者 display_errors = On, Off ...
- linux如何安装yum
yum全称Yellow dog Updater Modified,yum的主要用途是对rpm包进行管理,包括安装.卸载.升级等.linux安装yum也较为简单,具体如下: 工具/原料 1.电脑: 2. ...
- vue 打印
vue 方法 第一种方法:通过npm 安装插件 1,安装 npm install vue-print-nb --save 2,引入 安装好以后在main.js文件中引入 import Print ...
- Python学习 day15
一.内置函数(共68个) 1.作用域相关(2) locals(*args, **kwargs) -- 返回本地作用域中的所有名字 globals(*args, **kwargs) -- 返回全 ...
- 2019.04.18 读书笔记 深入string
整个.net中,最特殊的就是string类型了,它有着引用类型的特性,又有着值类型的操作方式,最特殊的是,它还有字符串池,一个字符串只会有一个实例(等下就推翻!). 鉴于之前的<==与Equal ...
- XML 十六进制值 是无效的字符错误 解决方法之一 转
/// <summary> /// 过滤非打印字符 /// </summary> /// <param name="tmp">待过滤</p ...