public void Init() { Reset(); transform.localScale = new Vector3(Scale * transform.localScale.x, Scale * transform.localScale.y, Scale * transform.localScale.z); if (Target == null) { Destroy(gameObject); return; } ParticleSystem[] pars = GetComponentsInChildren <ParticleSystem>(); foreach (ParticleSystem par in pars) { par.startSize *= Scale; } _cacheTranform = Target.FindInChildren(DummyPoint.ToString()); if (_cacheTranform == null) { _cacheTranform = Target.transform; } if (_cacheTranform == null) { Destroy(gameObject); return; } onInit(); Vector3 pos = _cacheTranform.TransformPoint(OffsetPos); transform.position = pos; if (RotateToAttacker && Attacker && Target) { Vector3 dir = Target.transform.position - Attacker.transform.position; dir.y = 0; dir.Normalize(); transform.right = Vector3.Cross(dir, transform.up); transform.forward = dir; } else { Quaternion quater = Quaternion.Euler(OffsetRotate.x, OffsetRotate.y, OffsetRotate.z); Matrix4x4 mat = _cacheTranform.localToWorldMatrix; Vector4 vy = mat.GetColumn(1); Vector4 vz = mat.GetColumn(2); Quaternion wq = Quaternion.LookRotation(new Vector3(vz.x, vz.y, vz.z), new Vector3(vy.x, vy.y, vy.z)); transform.rotation = wq * quater; } onPostInit(); }
override protected void onUpdate() { if (TraceType == eTraceType.eTraceType_Targeter) { if (Target != null) { Transform targetObj = Target.FindInChildren(TraceDummyPoint.ToString()); if (targetObj == null) { targetObj = Target.transform; } _lastTracePos = targetObj.position; } } float fRestDis = Vector3.Distance(_lastTracePos, _TraceStartPos); Vector3 dir = _lastTracePos - _TraceStartPos; dir.Normalize(); _traceMovedDis += Speed * Time.deltaTime; _curTraceOffsetRate = _traceMovedDis / (_traceMovedDis + fRestDis); if (Speed * Time.deltaTime >= Vector3.Distance(_TraceStartPos, _lastTracePos)) { OnTracedTarget(); return; } _TraceStartPos += dir * Speed * Time.deltaTime; float fYOffset = 0f; float fRate = _curTraceOffsetRate <= 0f ? 0f : (_curTraceOffsetRate <= 0.5f ? (_curTraceOffsetRate / 0.5f) : (1f - _curTraceOffsetRate) / 0.5f); float fOffRate = TraceStandDis <= 0 ? 1f : (_traceMovedDis + fRestDis) / TraceStandDis; fYOffset = (TraceOffsetLen * fOffRate * fRate - TraceOffsetLen * fOffRate * fRate * fRate / 2) * 2; //fYOffset = fRate * TraceOffsetLen; Vector3 vecUp = Vector3.Cross(dir, Vector3.up); vecUp = Vector3.Cross(vecUp, dir); Quaternion quater = Quaternion.Euler(0, 0, TraceOffsetAngle); vecUp = quater * vecUp; vecUp.Normalize(); vecUp *= fYOffset; Vector3 curPos = _TraceStartPos; curPos += vecUp; transform.position = curPos; Vector3 tmpVecUp = Vector3.up * TraceOffsetLen * fOffRate; tmpVecUp = tmpVecUp - tmpVecUp * 2f * _curTraceOffsetRate; tmpVecUp = quater * tmpVecUp; Vector3 tmpVecFor = _lastTracePos - _TraceStartPos; tmpVecFor.Normalize(); if (TraceOffsetLen > 0f) { tmpVecFor *= TraceOffsetLen * fOffRate * FactorVal; } transform.forward = tmpVecUp + tmpVecFor; }