protected override void InputUpdate() { if (HasTarget()) { if (!isCombat) { Vector3 targetPos = weapons.WeaponsTargetSelector.SelectedTarget.transform.TransformPoint(weapons.WeaponsTargetSelector.SelectedTarget.TrackingBounds.center); if (!combatDelayed && Vector3.Angle(torsoGimbal.GimbalController.HorizontalPivot.forward, targetPos - torsoGimbal.GimbalController.HorizontalPivot.position) < 45) { StartCoroutine(CombatDelay()); Combat(); } else { Patrol(); } } else { Combat(); } } else { Patrol(); } moveTargetPosition.y = characterController.transform.position.y; Vector3 localLegsMovementDirection = characterController.transform.InverseTransformDirection(moveTargetPosition - characterController.transform.position).normalized; Vector3 localLookMovementDirection = torsoGimbal.GimbalController.HorizontalPivot.InverseTransformDirection(moveTargetPosition - characterController.transform.position).normalized; float angle = Vector3.Angle(characterController.transform.forward, moveTargetPosition - characterController.transform.position); if (characterController.transform.InverseTransformPoint(moveTargetPosition).x < 0) { angle *= -1; } float turnAmount = 0; bool reversing = localLookMovementDirection.z < -0.01f; if (localLegsMovementDirection.magnitude > 0.01f) { Vector3 localLegsTargetDirection = localLegsMovementDirection; if (reversing) { localLegsTargetDirection *= -1; } turnAmount = Mathf.Atan2(localLegsTargetDirection.x, localLegsTargetDirection.z) * 10; } characterController.SetRotationInputs(new Vector3(0f, turnAmount, 0)); characterController.SetReversing(true); characterController.SetMovementInputs(localLegsMovementDirection.normalized); }
// Called every frame that this input script is active. protected override void InputUpdate() { // Get the movement input float forward = Input.GetAxis("Vertical"); float right = Input.GetAxis("Horizontal"); // Get the target input vector Vector3 movementInputs = Vector3.ClampMagnitude(new Vector3(right, 0f, forward), 1); // Get the next input vector movementInputs = Vector3.Lerp(lastMovementInputs, movementInputs, (1 / (1 + movementSmoothing))); lastMovementInputs = movementInputs; // Update whether the character is reversing bool reversing = movementInputs.z < -0.01f; m_RigidbodyCharacterController.SetReversing(reversing); // Convert the input to a local movement vector Vector3 worldMovementDirection; if (lookController != null) { worldMovementDirection = lookController.GimbalController.HorizontalPivot.TransformDirection(movementInputs).normalized; } else { worldMovementDirection = Camera.main.transform.TransformDirection(movementInputs).normalized; } Vector3 localMovementDirection = m_RigidbodyCharacterController.transform.InverseTransformDirection(worldMovementDirection); // Send movement inputs m_RigidbodyCharacterController.SetMovementInputs(localMovementDirection * movementInputs.magnitude); // Get the jump input if (Input.GetKeyDown(KeyCode.Space)) { if (m_RigidbodyCharacterController.Grounded) { m_RigidbodyCharacterController.Jump(); } else { m_RigidbodyCharacterController.ActivateJetpack(); } } // Deactivate jetpack on release of input if (Input.GetKeyUp(KeyCode.Space) && m_RigidbodyCharacterController.Jetpacking) { m_RigidbodyCharacterController.DeactivateJetpack(); } // Set whether the character is running m_RigidbodyCharacterController.SetRunning(Input.GetKey(KeyCode.LeftShift)); // Rotate the character to face the movement vector float turnAmount = 0; if (movementInputs.magnitude > 0.01f) { Vector3 localLegsTargetDirection = localMovementDirection; if (reversing) { localLegsTargetDirection *= -1; } turnAmount = Mathf.Atan2(localLegsTargetDirection.x, localLegsTargetDirection.z) * 10; } // Send rotation inputs to the character m_RigidbodyCharacterController.SetRotationInputs(new Vector3(0f, turnAmount, 0f)); }