// Update is called once per frame void Update() { if (targetPosition == Vector3.positiveInfinity || Speed == 0f || !started) { return; } if (Target != null) { targetPosition = Target.position; } float curDistance = Vector3.Distance(targetPosition, transform.position); float distanceRatio = Mathf.Clamp(curDistance / startingDistance, 0f, 1f); if (FadeOutSpeed) { curspeed = FadeSpeedCurve.Evaluate(distanceRatio) * Speed; } if (IncreaseTurnSmoothing) { curTurnSmoothing = TurnSmoothingCurve.Evaluate(distanceRatio) * TurnSmoothing; } Vector3 Direction = Vector3.Normalize(targetPosition - transform.position); if (curDirection == Vector3.zero) { curDirection = Vector3.Normalize(startingDirection); } else { curDirection = Vector3.Lerp(curDirection, Direction, Time.deltaTime * curTurnSmoothing); } // Vector3 smoothTurnDirection = Vector3.Lerp(transform.forward, Direction, Time.deltaTime * curTurnSmoothing); // Vector3 smoothTurnDirection = Vector3.Lerp(curDirection, Direction, Time.deltaTime * curTurnSmoothing); // transform.position += transform.forward * curspeed * Time.deltaTime; transform.position += curDirection * curspeed * Time.deltaTime; transform.rotation = Quaternion.LookRotation(curDirection, Vector3.up); // smoothTurnDirection if (curDistance <= StopDistance) { OnReach?.Invoke(); started = false; if (DeactivateOnReach) { this.gameObject.SetActive(false); } } }
protected void InvokeOnReach(Player player) { OnReach?.Invoke(player); }