void Update() { // Lerp agent speed for a more organic effect currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, velocityLerpSpeed * Time.deltaTime); agent.speed = currentSpeed * movementSpeed.GetMultiplier(); anim.SetFloat("Speed", (agent.velocity.magnitude) / (chaseSpeed - walkSpeed)); // Different update depending on the current state StateUpdate(); // Check if the human has reach his destination if (Vector3.Distance(transform.position, agent.destination) < navTreshold) { StateDestinationReached(); } }
void CheckMovementInputs(GamePadState state) { var rightStickAmplitude = GetStickDirection(state.ThumbSticks.Right).magnitude; var leftStickAmplitude = GetStickDirection(state.ThumbSticks.Left).magnitude; // Calculate the direction of the movement depending on the gamepad inputs Vector2 direction = new Vector2(state.ThumbSticks.Left.X, state.ThumbSticks.Left.Y); targetSpeed = direction.magnitude * speed; currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, Time.deltaTime * speedLerpSpeed); float weightSpeedMultiplier = 1f; if (IsHolding()) { weightSpeedMultiplier = Mathf.Clamp(carryCapacity - heldObject.weight, 0f, 1f) * (1f - minimumWeightedSpeedFactor) + minimumWeightedSpeedFactor; } sweat.Set(1 - weightSpeedMultiplier); transform.position += Vector3.ClampMagnitude(new Vector3(direction.x, 0f, direction.y), 1f) * currentSpeed * Time.deltaTime * movementSpeed.GetMultiplier() * weightSpeedMultiplier; anim.SetFloat("Speed", targetSpeed / speed); // If the player is aiming if (rightStickAmplitude > 0.1f) { targetOrientation = new Vector3(state.ThumbSticks.Right.X, 0f, state.ThumbSticks.Right.Y); } else if (leftStickAmplitude > 0.1f) { targetOrientation = new Vector3(state.ThumbSticks.Left.X, 0f, state.ThumbSticks.Left.Y); } // Rotate the character towards his movement direction if (targetOrientation.magnitude >= 0.1f) { transform.forward = Vector3.Lerp(transform.forward, targetOrientation, Time.deltaTime * orientationLerpSpeed); } }