文章目录Mathf的clamplerp代码实现跟踪方法2
Mathf的剪辑
clamp是限制Unity中轴向移动范围的函数
在游戏中,为了将玩家的某个轴向运动限制在一定范围之外,可以通过Mathf.Clamp解决
mathf.clamp(floatvalue,float min,float max ) )。
将三个参数:value、min和max传递给Mathf.Clamp
限制值在min、max之间,如果value大于max则返回max,如果value小于min则返回min,否则返回value;
lerp public static Vector3 Lerp (Vector3 a, Vector3 b, float t);
参数
A的起始值。 返回t=0时。
b结束值,t=1时返回。
t是用于在a和b之间插值的值。
返回
Vector3插值等于a(b-a ) * t。
说明
在两点之间进行线性插值。
使用插值t在点a和点b之间插值。 参数t被限制在范围[ 0,1 ]内。 它最常用于查找占两个终端之间距离特定百分比的点。 例如,在这些点之间逐步移动对象。
返回的值为a(b-a ) (t )。 也可以写成a ) ) (1-t ) b*t )。
t=0时,vector3.lerp(a,b,t )返回/a/。
如果t=1,vector3.lerp(a,b,t )将返回/b/。
如果t=0.5,则vector3.lerp(a,b,t )返回a和b之间的中点。
transform.position=vector3. lerp (transform.position.x,targetPosition,Time.deltaTime ); //看起来像是等速移动代码的实现,首先编写C#脚本,然后制作天空物体,将脚本挂在天空物体上,将照相机作为其子物体。
原理是将摄像机固定在一定范围内,使用Lerp函数追踪目标的移动
拖动应该追踪的目标,进行范围设定。 这里的范围限制可以根据情况设定。 如上所述,可以进行编码以实现摄像机的跟踪
using System.Collections; using system.collections.generic; using UnityEngine; publicclasscamerafollow 3360 mono behaviour { publictransformplayer; //[ serialize field ]//publicfloatsmooth; //startiscalledbeforethefirstframeupdatevoidstart () { player=GameObject.Find ('踏实的发箍').GetComponentTransform //updateiscalledonceperframevoidupdate ()//写法统一transform.position=new vector3(player.position.y.x,player //transform.position=vector2. lerp (transform.position,camerafollow,smooth ); }方法二一(不推荐) ) ) ) ) ) ) ) )0)
将摄影机挂在player下()太低了) )。
二
将cinermachine添加到包管理器;
然后,在cineramachine中添加2d摄像头,将player拖动到VM vcam1中即可