71 lines
2.6 KiB
C#
71 lines
2.6 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
|
||
using UnityEngine;
|
||
|
||
public class FastSlowAnimationANDZoomIN : MonoBehaviour
|
||
{
|
||
public Camera cam;
|
||
public Camera workerCamera;
|
||
//Animator anim;
|
||
|
||
public float maxHeight = 2f; // 最大上升高度(米)
|
||
float scaleRation = 0.5f;//默认缩放系数
|
||
public float upTime = 1.5f;// 上升到最高点所需时间(秒)
|
||
|
||
private Dictionary<Transform, Vector3> childrenStartPos = new Dictionary<Transform, Vector3>();
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
cam = Camera.main;
|
||
// workerCamera = GameObject.Find("WorkerCamera").GetComponent<Camera>();zhe行代码为什么会引起 bug?目前还不清楚
|
||
|
||
foreach (Transform child in transform)
|
||
{
|
||
childrenStartPos[child] = child.position;
|
||
|
||
}
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
if (cam.gameObject.activeInHierarchy == true)
|
||
{
|
||
//自由视角下的动画逻辑
|
||
foreach (Transform child in transform)//每个孩子就是一个世界空间的大画布
|
||
{
|
||
//通过遍历每个子物体,每个物体的大小是独立计算的,不能取最小值
|
||
float dist = Vector3.Distance(child.position, cam.transform.position);
|
||
float scale = scaleRation * dist / 30f;//这里给改成0.1m是最近距离吧,缩放到0.00333倍,10倍是300米
|
||
scale = Mathf.Clamp(scale, 0.00333f, 10f);
|
||
|
||
maxHeight = 0.01f + (dist - 0.1f) * 0.99f / 49.9f;//0.01米设置为最低吧
|
||
maxHeight = Mathf.Clamp(maxHeight, 0.01f, 2f);
|
||
|
||
float yOffset = Mathf.PingPong(Time.time * (maxHeight / upTime), maxHeight);
|
||
|
||
child.position = childrenStartPos[child] + new Vector3(0, yOffset, 0);
|
||
child.localScale = Vector3.one * scale;
|
||
child.LookAt(child.position + cam.transform.forward);//看相机
|
||
//transform.LookAt(transform.position + target.forward);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//切换到工人视角下所放和浮动在这里
|
||
foreach (Transform child in transform)//每个孩子就是一个世界空间的大画布
|
||
{
|
||
|
||
|
||
float yOffset = Mathf.PingPong(Time.time * (0.3f / upTime), 0.3f);
|
||
|
||
child.position = childrenStartPos[child] + new Vector3(0, yOffset, 0);
|
||
child.localScale = Vector3.one * 0.3f;
|
||
child.LookAt(child.position + workerCamera.transform.forward);//看相机
|
||
//transform.LookAt(transform.position + target.forward);
|
||
}
|
||
}
|
||
}
|
||
}
|