这里给主相机绑定一个脚本。

脚本写为:

using UnityEngine;
using System.Collections; public class camerafollow : MonoBehaviour {
//主摄像机跟随主角一起移动
public float xMargin = 1f;
public float yMargin = 1f;
public float xSmooth = 8f;
public float ySmooth = 8f;
public Vector2 maxXandY;
public Vector2 minXandY;
// Use this for initialization
private Transform player;
void Start () {
//这里获得是绑定的主角,需要一起跟随移动,就要获得主角的属性
player = GameObject.FindGameObjectWithTag("pk_0").transform;
maxXandY.x = 10; maxXandY.y = 10;
}
bool checkxmargin() {
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
bool checkymargin() {
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}
// Update is called once per frame
void Update () {
Trackplayer();
}
//跟踪主角
void Trackplayer() {
float targetx = transform.position.x;
float targety = transform.position.y;
if (checkxmargin())
{
targetx = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime); }
if (checkymargin())
{
targety = Mathf.Lerp(transform.position.y, player.position.y, xSmooth * Time.deltaTime);
}
targetx = Mathf.Clamp(targetx, minXandY.x, maxXandY.y);
targety = Mathf.Clamp(targety, minXandY.y, maxXandY.y);
transform.position = new Vector3(targetx, targety,transform.position.z);
}
}

  效果图:

Unity3D 相机跟随主角移动的更多相关文章

  1. Unity3D——相机跟随物体移动

    public Transform target; public float moveSmooth=5f; Vector3 offset; void Start () { offset = transf ...

  2. [原]unity3D 相机跟随

    using UnityEngine;using System.Collections; public class CameraFollow : MonoBehaviour {            p ...

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

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

  4. Unity3D实现摄像机视野的拉远拉近和跟随主角旋转效果

    在Unity官网教程SurvivalShooter(恶魔射手)中,只处理了主角跟随鼠标旋转,摄像机视野并没有旋转或通过滚轮实现视野的拉远拉近,一下是我的实现方法.   在教程中,主角的移动是通过    ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. POJ Code the Tree 树的pufer编号

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2259   Accepted: 859 Desc ...

  2. [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】

    题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...

  3. java cpu缓存

    众所周知, CPU是计算机的大脑, 它负责执行程序的指令; 内存负责存数据, 包括程序自身数据. 同样大家都知道, 内存比CPU慢很多. 其实在30年前, CPU的频率和内存总线的频率在同一个级别, ...

  4. CountDownLatch的使用

    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.主要方法public CountDownLatch(int count);publi ...

  5. 14.5.4 Phantom Rows 幻影行

    14.5.4 Phantom Rows 幻影行 所谓的幻读问题发生在一个事务 当相同的查询产生不同的结果集在不同的时间. 例如,如果一个SELECT 是执行2次,但是第2次返回的时间不第一次返回不同, ...

  6. C 常用的输入输出 栈 哈希 文件写读 实现 字符串处理

    #include "stdafx.h"#include <stdio.h>#include <string.h>#include <stdlib.h& ...

  7. codeforces #268 div2 D

    对于这道题第一感觉是图论相关然后我们先分析,假设a[i]在A集合需要的元素是a[x],在B集合是a[y]那么假设a[i]在A集合,那必然a[x]也必须在A集合,由于a[y]如果在B集合就没有对应元素, ...

  8. 深入浅出Node.js (附录A) - 安装Node

    A.1 Windows系统下的Node安装 A.2 Mac系统下Node的安装 A.3 Linux系统下Node的安装 A.4 总结 A.5 参考资源

  9. Linux Shell编程(29)——函数

    和"真正的"编程语言一样, Bash也有函数,虽然在某些实现方面稍有些限制. 一个函数是一个子程序,用于实现一串操作的代码块,它是完成特定任务的"黑盒子". 当 ...

  10. HDOJ1020 Encoding

    Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...