Unity物体跟随脚本
2015-01-06 10:55
393 查看
using UnityEngine;
using System.Collections;
public class demo : MonoBehaviour
{
//跟随目标
public Transform TargetObj = null;
//平滑速度,使用线性差值运算使跟随者平滑的移动到目标位置
public float SmoothSpeed = 3;
//设置最高点
public bool UseMaxHeight = false;
//最高点
public float MaxHeight = 0;
//设置最低点
public bool UseMinHeight = false;
//最低点
public float MinHeight = 0;
//初始时与跟随对象的相对距离
private Vector3 initDis;
void Awake()
{
if (TargetObj == null)
{
throw new UnityException("The TargetObj can't be null!");
}
initDis = new Vector3(this.transform.position.x - TargetObj.position.x,
this.transform.position.y - TargetObj.position.y,
this.transform.position.z - TargetObj.position.z);
}
void Start()
{
//检查最大高度和最小高度的合法性
if (UseMaxHeight && UseMinHeight)
{
if (MaxHeight <= MinHeight)
{
throw new UnityException("The MaxHeight must larger than MinHeight!");
}
}
}
// Update is called once per frame
void Update()
{
float zValue = TargetObj.position.z + this.initDis.z;
if (UseMaxHeight && zValue > MaxHeight)
{
zValue = MaxHeight;
}
if (UseMinHeight && zValue < MinHeight)
{
zValue = MinHeight;
}
Vector3 targetPos = new Vector3(TargetObj.position.x + this.initDis.x,
TargetObj.position.y + this.initDis.y,
zValue);
this.transform.position = Vector3.Lerp(this.transform.position,
targetPos,
this.SmoothSpeed);
}
}
using System.Collections;
public class demo : MonoBehaviour
{
//跟随目标
public Transform TargetObj = null;
//平滑速度,使用线性差值运算使跟随者平滑的移动到目标位置
public float SmoothSpeed = 3;
//设置最高点
public bool UseMaxHeight = false;
//最高点
public float MaxHeight = 0;
//设置最低点
public bool UseMinHeight = false;
//最低点
public float MinHeight = 0;
//初始时与跟随对象的相对距离
private Vector3 initDis;
void Awake()
{
if (TargetObj == null)
{
throw new UnityException("The TargetObj can't be null!");
}
initDis = new Vector3(this.transform.position.x - TargetObj.position.x,
this.transform.position.y - TargetObj.position.y,
this.transform.position.z - TargetObj.position.z);
}
void Start()
{
//检查最大高度和最小高度的合法性
if (UseMaxHeight && UseMinHeight)
{
if (MaxHeight <= MinHeight)
{
throw new UnityException("The MaxHeight must larger than MinHeight!");
}
}
}
// Update is called once per frame
void Update()
{
float zValue = TargetObj.position.z + this.initDis.z;
if (UseMaxHeight && zValue > MaxHeight)
{
zValue = MaxHeight;
}
if (UseMinHeight && zValue < MinHeight)
{
zValue = MinHeight;
}
Vector3 targetPos = new Vector3(TargetObj.position.x + this.initDis.x,
TargetObj.position.y + this.initDis.y,
zValue);
this.transform.position = Vector3.Lerp(this.transform.position,
targetPos,
this.SmoothSpeed);
}
}
相关文章推荐
- Unity脚本,平滑跟随目标物体移动和旋转
- unity摄像机跟随的脚本
- Unity 使用备忘(四) 游戏物体不挂在脚本文件上直接实现Button(Script)脚本文件的Onclick方法
- Unity学习笔记——利用脚本实现对一个物体的第三人称观察
- Unity 3d 实现物体跟随摄像机视野运动
- Unity 3D 控制物体前后左右均匀移动脚本
- unity 自带脚本smoothfollow的修改,实现不同方向的跟随
- unity物体的移动/脚本的添加
- unity 赛车相机跟随物体移动
- 【Unity】物体跟随鼠标移动
- Unity脚本使用:物体间通信方式
- unity中同一个物体上挂载多个脚本的执行顺序
- unity多个物体间的脚本执行顺序
- 面向组件编程之Unity 3.怎样获取脚本所在物体的各种组件 例如:GetComponentInChildren<T>()/GetComponentsInChildren<U>()
- Unity物体跟随鼠标点的位置自动寻路
- Unity物体之间、脚本之间的参数传递
- 面向组件编程之Unity 1.怎样获取脚本所在物体的各种组件,例如:transform/BoxCollider
- 【Unity】获得当前脚本的物体的transform和gameobject
- Unity入门系列||Unity中如何用脚本实现物体的循环移动和旋转
- Unity属性——AddComponentMenu 字面理解:添加 组件选项菜单 分析:可能是添加一个脚本或者组件到一个物体上 验证: 新建一个脚本:AttributeTest 提示:添