using System; using System.Collections.Generic; using UnityEngine; /// /// Transform组件助手类 /// class TransformHelper { /// /// 面向目标方向 /// /// 目标方向 /// 需要转向的对象 /// 转向速度 public static void LookAtTarget(Vector3 targetDirection, Transform transform,float rotationSpeed) { if (targetDirection != Vector3.zero) { var targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed); } } /// /// 查找子物体(递归查找) /// /// 父物体 /// 子物体的名称 /// 找到的相应子物体 public static Transform FindChild(Transform trans, string goName) { if (goName == trans.name) return trans; Transform child = trans.Find(goName); if (child != null) return child; Transform go = null; for (int i = 0; i < trans.childCount; i++) { child = trans.GetChild(i); go = FindChild(child, goName); if (go != null) return go; } return null; } }