当前位置:网站首页>Unity2D--相机跟随
Unity2D--相机跟随
2022-07-15 18:16:00 【xuzhongzheng123】
这里需要实现的相机跟随是指人物与相机距离超过一定范围才会使相机跟随主角。
实现这个功能想明白以下2个函数。
Mathf.Abs
这个函数需要提供2个数,然后返回他们差的绝对值
Mathf.Lerp
这个函数需要提供3个参数,第3个参数范围为0~1,能返回一个数,比如
(a,b,c)返回的数为a+(b-a)*c
那么依据这两个函数可以实现想要的效果,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Followcamera : MonoBehaviour
{
private Transform player;
private float xMargin = 0.4f;
private float yMargin = 0.4f;
private float xSmooth = 5f;
private float ySmooth = 5f;
public Vector2 maxXAndY ;
public Vector2 minXAndY ;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
bool CheckXMargin() {
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
bool CheckYMargin() {
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}
void Trackplayer() {
float targetX=transform.position.x;
float targetY=transform.position.y;
Debug.Log(Time.deltaTime);
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, ySmooth*Time.deltaTime );
}
transform.position = new Vector3(targetX,targetY,transform.position.z);
}
void FixedUpdate()
{
Debug.Log(Mathf.Lerp(9.0F, 1.0F, 0.1F));
Trackplayer();
}
}
边栏推荐
猜你喜欢

Selenium使用之解决‘chromedriver‘ executable needs to be in PATH.报错信息

图片待传递

Logic of archives | holonomic distinction and examples

Broadcast mechanism in pytoch

Torch in pytoch Unsqueeze() and torch Squeeze() function parsing

SAP ABAP 系统进行数据库表查询的几种常用方法的试读版

Qt(四)使用代码与UI文件混合开发

Torch in pytoch Analysis of nonzero() function

JVM memory scaling

微软这个系统,90% 的人都没用过!
随机推荐
开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
MySQL安装时一直卡在starting the server的解决方案
If in SQL determines the error
Connecting with enterprise wechat, customer relationship management can also be very simple!
STL值string学习
PostgreSQL自带的单机并行查询的执行方法是什么?
浩辰CAD建筑版 2022软件安装包下载及安装教程
家居建材企业如何打造智慧供应链管理体系?数商云供应链系统深耕采购、物流多业务应用场景
How do I open an account with tongdaxin? Is it safe to open a stock account by mobile phone?
Common differences between MySQL and Oracle (I)
第四讲:最长上升子串
go 语言学习笔记(1)
Li Mu hands on deep learning V2 target detection data set
Broadcast mechanism in pytoch
Torch in pytoch numel(),torch. shape,torch. Size () and torch Reshape() function parsing
C语言:数的阶乘递归实现
How can home building materials enterprises build a smart supply chain management system? Digital commerce cloud supply chain system focuses on multi business application scenarios of procurement and
mysql 报错 mysqld:sort aborted:Server shutdown in progress 原因
全面解读数据中台,让企业实现数字化转型
R语言使用reshape2包的melt函数进行dataframe变形将dataframe数据从宽表变换为长表、dcast函数把melt函数处理后的数据、基于一个自定义公式(formula)从长表到宽表