private void FixedUpdate() { switch (currentState) { case MotoState.Default: if (InTransit) { DistanceToTarget = (Destination - transform.position).sqrMagnitude; Velocity = (Destination - transform.position).normalized * PatrolSpeed * SpeedOverDistance.Evaluate(DistanceToTarget / _maxDistance); if ((Destination - transform.position).sqrMagnitude <= 0.01f) { Velocity = Vector3.zero; InTransit = false; } //Set rotation if (Velocity.sqrMagnitude > 0.1f) { Vector3 LookDir = Vector3.ProjectOnPlane(Velocity, transform.up); Forward = Vector3.Slerp(Forward, LookDir, Time.fixedDeltaTime * RotationSpeed); Quaternion Rotation = Quaternion.LookRotation(Forward); rigidBody.MoveRotation(Rotation); } } break; default: currentState = MotoState.Default; break; } float Height = col.radius / 2; if (Physics.Raycast(rigidBody.position, -transform.up, out RaycastHit ground, Height + GroundCheckOvershoot, GroundLayer)) { Normal = ground.normal; GroundPoint = ground.point + ground.normal * Height; } rigidBody.MovePosition(rigidBody.position + Velocity * Time.fixedDeltaTime); }
// Update is called once per frame void Update() { switch (currentState) { case MotoState.Default: animator.SetFloat("Speed", Velocity.magnitude); if (!InTransit) { t += Time.deltaTime; if (t >= PatrolDelay) { Destination = PatrolPoint(Home, PatrolRadius); _maxDistance = (Destination - transform.position).sqrMagnitude; t = 0; InTransit = true; } } break; default: currentState = MotoState.Default; break; } }