private void HandleCharacter() { m_cameraForward.Set(1, 0, 1); // Look direction. m_lookDirection = Vector3.Scale(m_camera.forward, m_cameraForward).normalized; m_lookDirection.y = 0; // Look rotation m_lookRotation = Quaternion.FromToRotation(m_transform.forward, m_lookDirection); //m_lookRotation = CalculateTargetRotation(m_lookDirection); // Input vector. m_inputVector.Set(moveInput.x, 0, moveInput.y); //Quaternion targetRotation = Quaternion.FromToRotation(m_transform.forward, lookDirection); m_controller.Move(moveInput.x, moveInput.y, m_lookRotation); }
public override TaskStatus OnUpdate() { var target = GetDefaultGameObject(targetGameObject.Value); if (target != prevTarget) { controller = target.GetComponentInParent <RigidbodyCharacterController>(); prevTarget = target; } if (controller == null) { return(TaskStatus.Failure); } controller.Move(horizontalMovement.Value, forwardMovement.Value, Quaternion.LookRotation(direction.Value)); return(TaskStatus.Success); }
/// <summary> /// Move according to the NavMeshAgent velocity. /// </summary> protected virtual void FixedUpdate() { var velocity = Vector3.zero; var lookRotation = Quaternion.LookRotation(m_Transform.forward); if (m_NavMeshAgent.isOnOffMeshLink) { UpdateOffMeshLink(ref velocity, ref lookRotation); } else { // Only move if a path exists. if (m_NavMeshAgent.desiredVelocity.sqrMagnitude > 0.01f) { if (m_NavMeshAgent.updateRotation) { lookRotation = Quaternion.LookRotation(m_NavMeshAgent.desiredVelocity); } else { lookRotation = Quaternion.LookRotation(m_Transform.forward); } // The normalized velocity should be relative to the look direction. velocity = Quaternion.Inverse(lookRotation) * m_NavMeshAgent.desiredVelocity; // Only normalize if the magnitude is greater than 1. This will allow the character to walk. if (velocity.sqrMagnitude > 1) { velocity.Normalize(); // Smoothly come to a stop at the destination. if (m_NavMeshAgent.remainingDistance < 1f) { velocity *= m_ArriveRampDownCurve.Evaluate(1 - m_NavMeshAgent.remainingDistance); } } } } // Don't let the NavMeshAgent move the character - the controller can move it. m_NavMeshAgent.updatePosition = false; m_NavMeshAgent.velocity = Vector3.zero; m_Controller.Move(velocity.x, velocity.z, lookRotation); m_NavMeshAgent.nextPosition = m_Transform.position; }
/// <summary> /// Call Move directly on the character. A similar approach could have been used as the CameraController/Handler where the RigidbodyCharacterController /// directly checks the input storage variable but this would not allow the RigidbodyCharacterController to act as an AI agent as easily. /// </summary> protected virtual void FixedUpdate() { #if ENABLE_MULTIPLAYER if (m_AllowGameplayInput && isLocalPlayer) { #else if (m_AllowGameplayInput) { #endif // Update the look rotation within FixedUpdate because the camera may change rotations multiple times within its FixedUpdate loop. if (!m_Controller.IndependentLook()) { if (m_Controller.Movement == RigidbodyCharacterController.MovementType.Combat || m_Controller.Movement == RigidbodyCharacterController.MovementType.Adventure || m_Controller.Movement == RigidbodyCharacterController.MovementType.FourLegged) { m_LookRotation = m_CameraTransform.rotation; } else if (m_Controller.Movement == RigidbodyCharacterController.MovementType.RPG) { if (m_PlayerInput.GetButton(Constants.SecondaryDisableButtonName)) { m_LookRotation = m_CameraTransform.rotation; } else if (!m_PlayerInput.GetButton(Constants.PrimaryDisableButtonName)) { if (m_ForwardMovement != 0 || m_HorizontalMovement != 0) { m_LookRotation = m_CameraTransform.rotation; } } } } } // The PointClickControllerHandler will move the character. if (m_Controller.Movement == RigidbodyCharacterController.MovementType.PointClick) { return; } m_Controller.Move(m_HorizontalMovement, m_ForwardMovement, m_LookRotation); }
void HandleMovement() { Vector3 moveDir = Vector3.zero; bool moved = false; if (_movementEnabled) { if (Input.GetKey(KeyCode.W)) { moveDir += Camera.main.transform.forward; moved = true; } if (Input.GetKey(KeyCode.A)) { moveDir -= Camera.main.transform.right; moved = true; } if (Input.GetKey(KeyCode.S)) { moveDir -= Camera.main.transform.forward; moved = true; } if (Input.GetKey(KeyCode.D)) { moveDir += Camera.main.transform.right; moved = true; } } if (_autowalk) { moveDir += Camera.main.transform.forward; moved = true; } if (moved) { _playerController.Move(moveDir); } }