Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本。
脚本写为:
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 相机跟随主角移动的更多相关文章
- Unity3D——相机跟随物体移动
public Transform target; public float moveSmooth=5f; Vector3 offset; void Start () { offset = transf ...
- [原]unity3D 相机跟随
using UnityEngine;using System.Collections; public class CameraFollow : MonoBehaviour { p ...
- unity3d简单的相机跟随及视野旋转缩放
1.实现相机跟随主角运动 一种简单的方法是把Camera直接拖到Player下面作为Player的子物体,另一种方法是取得Camera与Player的偏移向量,并据此设置Camera位置,便能实现简单 ...
- Unity3D实现摄像机视野的拉远拉近和跟随主角旋转效果
在Unity官网教程SurvivalShooter(恶魔射手)中,只处理了主角跟随鼠标旋转,摄像机视野并没有旋转或通过滚轮实现视野的拉远拉近,一下是我的实现方法. 在教程中,主角的移动是通过 ...
- unity3D:游戏分解之角色移动和相机跟随
游戏中,我们经常会有这样的操作,点击场景中某个位置,角色自动移动到那个位置,同时角色一直是朝向那个位置移动的,而且相机也会一直跟着角色移动.有些游戏,鼠标滑动屏幕,相机就会围绕角色旋转. ...
- unity_实用小技巧(相机跟随两个主角移动)
在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置.方法如下: public Trans ...
- SurvivalShooter学习笔记(一.相机跟随)
1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- Unity中几种简单的相机跟随
#unity中相机追随 固定相机跟随,这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collectio ...
随机推荐
- HDUOJ Clear All of Them I 状压DP
Clear All of Them I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Oth ...
- Servo: The Embeddable Browser Engine
Embedding, in the context of this article, is the process of hosting a web rendering engine inside a ...
- 【POJ】2155 Matrix
二维树状数组. /* poj2155 */ #include <iostream> #include <string> #include <map> #includ ...
- 谷歌浏览器怎么调试js
首先我们打开开发者工具,你可以直接在页面上点击右键,然后选择审查元素或者在Chrome的工具中找到或者你直接记住这个快捷方式: Ctrl+Shift+I (或者Ctrl+Shift+J直接打开控制台) ...
- Zookeeper、Solr和Tomcat安装配置实践
Zookeeper.Solr和Tomcat安装配置实践
- Java宝典
本人最近参加了几家公司的面试,在其中发现了不少笔试题,虽然是平常再简单不过的,但一不小心还是会出错.今天特意找时间写下来和大家分享. 1.访问控制符权限问题. 同一个包中 同一个类中 不同包的子类 ...
- winform 导出TXT 分类: WinForm 2014-05-15 15:29 128人阅读 评论(0) 收藏
截图: 代码实现:(导出txt按钮事件) using System.IO; using System.Data.OleDb; private void btnOutTxt_Click(object s ...
- spring security +spring boot 自定义 403 页面
用的spring security 做的权限控制, 当 访问没有权限, 跳转 会跳到默认403 页面.不符合当前项目需求. 一下是解决方式: package com.ycmedia; import ...
- Python基础知识---字典
现在在实习期间,好久没用Python了,今天在做Java项目时用的HashMap让我联想到了Python中的字典,就写一些Python字典的知识吧,复习复习. 字典: key --> valu ...
- jquery_EasyUI的学习
1 Accordion(可折叠标签) 1.1 实例 1.1.1 代码 <html> <head> <meta http-equiv="Content-Type& ...