相机路径跟随的效果结果如下:
【实现策略】
1.记录相机与相机方向的运动路径
2.根据进度条的%值改变相机运动与方向值在总路径中所处的%
3.ValueTo()可实现直接跳转到某一%所处的位置,只需在回调函数中改变%的值即可
【项目结构】
【源代码】
public class CameraController : MonoBehaviour {
public Transform[] movePath;
public Transform[] lookPath;
public Transform lookTarget;
public float process;
private bool isSe = false;
private void FixedUpdate()
{
if (isSe) {
process = process - 0.002f;
}
else
process = process + 0.002f;
if (process >= 1 || process <= 0) {
process = (int)process;
isSe = !isSe;
}
}
private void OnGUI()
{
process = GUI.HorizontalSlider(new Rect(50, 50, 300, 30),process, 0, 1);
iTween.PutOnPath(lookTarget, lookPath, process);
iTween.PutOnPath(gameObject, movePath, process);
transform.LookAt(iTween.PointOnPath(lookPath, process));
}
private void OnDrawGizmos()
{
iTween.DrawPath(movePath, Color.magenta);
iTween.DrawPath(lookPath, Color.cyan);
Gizmos.color = Color.black;
Gizmos.DrawLine(transform.position, lookTarget.position);
}
}
结合ValueTo
public class CameraController : MonoBehaviour {
public Transform[] movePath;
public Transform[] lookPath;
public Transform lookTarget;
public float process;
private bool isSe = false;
private void OnGUI()
{
process = GUI.HorizontalSlider(new Rect(50, 50, 300, 30),process, 0, 1);
if(GUI.Button(new Rect(50, 80, 100, 30),"转到中间")){
iTween.ValueTo(gameObject, iTween.Hash("from", process, "to", .5f, "time",2f,"onupdate", "MoveButton"));
}
iTween.PutOnPath(lookTarget, lookPath, process);
iTween.PutOnPath(gameObject, movePath, process);
transform.LookAt(iTween.PointOnPath(lookPath, process));
}
private void OnDrawGizmos()
{
iTween.DrawPath(movePath, Color.magenta);
iTween.DrawPath(lookPath, Color.cyan);
Gizmos.color = Color.black;
Gizmos.DrawLine(transform.position, lookTarget.position);
}
private void MoveButton(float p) {
process = p;
}
}
[Tip]
为一个游戏对象设定一个集合,并设置其在集合哪一%中
PutOnPath(GameObject target, Vector3[] path, float percent)
PutOnPath(GameObject target, Transform[] path, float percent)
当从 OnDrawGizmos()函数调用时,它将通过提供的Vector3或转换数组绘制一条曲线路径。
DrawPath(Vector3[] path)
DrawPath(Transform[] path)