Search Unity

Question how to disable PathFinder in script ?

Discussion in 'Input System' started by molo4nuk, Jan 11, 2023.

  1. molo4nuk

    molo4nuk

    Joined:
    Jan 9, 2021
    Posts:
    1
    How to disable PathFinder. I don't want it to generate paths by itself.

    how to make the same movement as in poe ?


    please help me fix it :(



    I need option 1. need enable
    I have option 2. need disable

    Code (CSharp):
    1. #region LOGIC
    2.  
    3.         private void MovementLogic()
    4.         {
    5.             if (ControllerEssentials.HasMovementRestrictions() || !movementEnabled || stunned ||
    6.                 RPGBuilderUtilities.IsPointerOverUIObject()) return;
    7.             if (IsStanding()) return;
    8.             MoveInputType moveInputType = MovingInput();
    9.             if (!moveInputType.Valid) return;
    10.             agent.stoppingDistance = 0;
    11.             ClickInteractableResult clickInteractableResult = IsClickingOnInteractable();
    12.             if (clickInteractableResult.Valid && !moveInputType.Held)
    13.             {
    14.                 if (clickInteractableResult.CancelMovement)
    15.                 {
    16.                     LookAtCursor();
    17.                     return;
    18.                 }
    19.  
    20.                 agent.stoppingDistance = clickInteractableResult.StoppingDistance;
    21.             }
    22.  
    23.             if (!Physics.Raycast(playerCamera.ScreenPointToRay(Input.mousePosition), out var hit,
    24.                 maxGroundRaycastDistance, groundLayers)) return;
    25.             var destination = clickInteractableResult.Valid ? clickInteractableResult.InteractablePosition : hit.point;
    26.             bool validClick = true;
    27.             if (IsPathTooClose(destination)) return;
    28.             if (!IsPathAllowed(destination))
    29.             {
    30.                 ValidCoordinate newResult = closestAllowedDestination(destination);
    31.                 if (newResult.Valid)
    32.                 {
    33.                     destination = newResult.ValidPoint;
    34.                     validClick = false;
    35.                 }
    36.                 else
    37.                 {
    38.                     return;
    39.                 }
    40.             }
    41.  
    42.             TriggerNewDestination(destination);
    43.  
    44.             if ((!alwaysTriggerGroundPathFeedback && moveInputType.Held) || clickInteractableResult.Valid) return;
    45.             SpawnGroundPathMarker(destination, validClick);
    46.             PlayGroundPathAudio(validClick);
    47.         }
    48.  
    49.  
    50.         private ClickInteractableResult IsClickingOnInteractable()
    51.         {
    52.             ClickInteractableResult clickInteractableResult = new ClickInteractableResult();
    53.             if (!Physics.Raycast(playerCamera.ScreenPointToRay(Input.mousePosition), out var hit,
    54.                 maxGroundRaycastDistance)) return clickInteractableResult;
    55.  
    56.             IPlayerInteractable playerInteractable = hit.transform.gameObject.GetComponent<IPlayerInteractable>();
    57.             if (playerInteractable == null) return clickInteractableResult;
    58.             clickInteractableResult.Valid = true;
    59.             clickInteractableResult.InteractablePosition = hit.transform.position;
    60.             float curStopDistance = GetCurrentStoppingDistance();
    61.             RPGCombatDATA.INTERACTABLE_TYPE interactableType = playerInteractable.getInteractableType();
    62.             switch (interactableType)
    63.             {
    64.                 case RPGCombatDATA.INTERACTABLE_TYPE.None:
    65.                     clickInteractableResult.Valid = false;
    66.                     break;
    67.                 case RPGCombatDATA.INTERACTABLE_TYPE.AlliedUnit:
    68.                 case RPGCombatDATA.INTERACTABLE_TYPE.NeutralUnit:
    69.                 case RPGCombatDATA.INTERACTABLE_TYPE.EnemyUnit:
    70.                     float realStopDistance = interactableType == RPGCombatDATA.INTERACTABLE_TYPE.AlliedUnit
    71.                         ? interactionDefaultRange
    72.                         : curStopDistance;
    73.                     if (Vector3.Distance(transform.position, hit.transform.position) <= realStopDistance)
    74.                     {
    75.                         clickInteractableResult.CancelMovement = true;
    76.                     }
    77.                     else
    78.                     {
    79.                         clickInteractableResult.StoppingDistance = realStopDistance;
    80.                     }
    81.  
    82.                     break;
    83.                 case RPGCombatDATA.INTERACTABLE_TYPE.InteractableObject:
    84.                 case RPGCombatDATA.INTERACTABLE_TYPE.CraftingStation:
    85.                 case RPGCombatDATA.INTERACTABLE_TYPE.LootBag:
    86.                 case RPGCombatDATA.INTERACTABLE_TYPE.WorldDroppedItem:
    87.                     if (Vector3.Distance(transform.position, hit.transform.position) <= interactionDefaultRange)
    88.                     {
    89.                         clickInteractableResult.CancelMovement = true;
    90.                     }
    91.                     else
    92.                     {
    93.                         clickInteractableResult.StoppingDistance = interactionDefaultRange;
    94.                     }
    95.  
    96.                     break;
    97.             }
    98.  
    99.             if (clickInteractableResult.Valid)
    100.             {
    101.                 if (Vector3.Distance(transform.position, hit.transform.position) >= maxInteractionRange)
    102.                 {
    103.                     clickInteractableResult.CancelMovement = true;
    104.                     UIEvents.Instance.OnShowAlertMessage("This is too far", 3);
    105.                     return clickInteractableResult;
    106.                 }
    107.  
    108.                 currentQueuedRPGBAction = new QueuedRPGBAction
    109.                 {
    110.                     InteractableType = interactableType, interactableREF = playerInteractable,
    111.                     go = hit.transform.gameObject
    112.                 };
    113.             }
    114.  
    115.             return clickInteractableResult;
    116.         }
    117.  
    118.         private float GetCurrentStoppingDistance()
    119.         {
    120.             if (GameState.playerEntity.autoAttackData.CurrentAutoAttackAbilityID == -1)
    121.                 return interactionDefaultRange;
    122.             RPGAbility.RPGAbilityRankData rankREF = GameState.playerEntity.GetCurrentAbilityRank(
    123.                 GameDatabase.Instance.GetAbilities()[GameState.playerEntity.autoAttackData.CurrentAutoAttackAbilityID],
    124.                 false);
    125.             return rankREF.maxRange;
    126.         }
    127.  
    128.         private void CameraLogic()
    129.         {
    130.             if (!cameraEnabled) return;
    131.             CameraInputs();
    132.             LerpCameraHeight();
    133.         }
    134.  
    135.         private void StandingLogic()
    136.         {
    137.             if (IsKeyDown(useActionKeys
    138.                 ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(standKeyActionKeyName)
    139.                 : standKey)) InitStanding();
    140.             if (IsStanding() || ControllerEssentials.isLockedOnCursor)
    141.             {
    142.                 HandleStanding();
    143.             }
    144.             else if (IsEndStanding())
    145.             {
    146.                 ResetStanding();
    147.             }
    148.         }
    149.  
    150.  
    151.         private void InitStanding()
    152.         {
    153.             ResetAgentActions();
    154.             StartCoroutine(SetCharacterState(CharacterState.Standing));
    155.         }
    156.  
    157.         private void ResetStanding()
    158.         {
    159.             StartCoroutine(SetCharacterState(CharacterState.Idle));
    160.         }
    161.  
    162.         public void ResetAgentActions()
    163.         {
    164.             agent.ResetPath();
    165.             agent.stoppingDistance = 0;
    166.             StartCoroutine(SetCharacterState(CharacterState.Idle));
    167.            
    168.             if (currentQueuedRPGBAction != null)
    169.             {
    170.                 TriggerQueuedRPGBAction();
    171.             }
    172.         }
    173.  
    174.         private void HandleStanding()
    175.         {
    176.             if (!charLookAtCursorWhileStanding) return;
    177.             LookAtCursor();
    178.         }
    179.  
    180.         public void LookAtCursor()
    181.         {
    182.             ValidCoordinate validCoordinate = GetGroundRayPoint();
    183.             if (!validCoordinate.Valid) return;
    184.             var targetRotation = Quaternion.LookRotation(validCoordinate.ValidPoint - transform.position);
    185.             targetRotation.x = 0;
    186.             targetRotation.z = 0;
    187.             //transform.rotation = targetRotation;
    188.             transform.rotation = Quaternion.Euler(0, targetRotation.eulerAngles.y, 0);
    189.         }
    190.  
    191.         public void LookAtPos(Vector3 pos)
    192.         {
    193.             var targetRotation = Quaternion.LookRotation(pos - transform.position);
    194.             targetRotation.x = 0;
    195.             targetRotation.z = 0;
    196.             transform.rotation = targetRotation;
    197.         }
    198.  
    199.         private ValidCoordinate GetGroundRayPoint()
    200.         {
    201.             var playerPlane = new Plane(Vector3.up, transform.position);
    202.             var ray = playerCamera.ScreenPointToRay(Input.mousePosition);
    203.             ValidCoordinate validCoordinate = new ValidCoordinate();
    204.             if (!playerPlane.Raycast(ray, out var hitDist)) return validCoordinate;
    205.             validCoordinate.Valid = true;
    206.             validCoordinate.ValidPoint = ray.GetPoint(hitDist);
    207.             return validCoordinate;
    208.         }
    209.  
    210.         public bool IsStanding()
    211.         {
    212.             return IsKeyHeld(useActionKeys
    213.                 ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(standKeyActionKeyName)
    214.                 : standKey);
    215.         }
    216.  
    217.         public bool IsEndStanding()
    218.         {
    219.             return IsKeyUp(useActionKeys
    220.                 ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(standKeyActionKeyName)
    221.                 : standKey);
    222.         }
    223.  
    224.         private void CharacterStateLogic()
    225.         {
    226.             switch (currentCharacterState)
    227.             {
    228.                 case CharacterState.Idle:
    229.                     break;
    230.  
    231.                 case CharacterState.Moving:
    232.                     if (IsDestinationReached()) ResetAgentActions();
    233.                     break;
    234.             }
    235.         }
    236.  
    237.         public IEnumerator SetCharacterState(CharacterState state)
    238.         {
    239.             yield return new WaitForEndOfFrame();
    240.             currentCharacterState = state;
    241.             StartAnimation(state);
    242.         }
    243.  
    244.         #endregion
    245.  
    246.         #region RPGBIntegration
    247.  
    248.         public void SetSpeed(float newSpeed)
    249.         {
    250.             agent.speed = newSpeed;
    251.             float speedMod = newSpeed / 5;
    252.             anim.SetFloat(moveSpeedModifier, speedMod);
    253.         }
    254.  
    255.         #endregion
    256.  
    257.         #region CAMERA
    258.  
    259.         void InstantCameraUpdate()
    260.         {
    261.             Vector3 targetPos = transform.position - (playerCamera.transform.forward * currentCameraHeight);
    262.             targetPos.z -= currentCameraVertical;
    263.             playerCamera.transform.position = targetPos;
    264.         }
    265.  
    266.         private void CameraInputs()
    267.         {
    268.             HandleCameraZoom();
    269.         }
    270.  
    271.         private void HandleCameraZoom()
    272.         {
    273.             if (Input.mouseScrollDelta.y == 0) return;
    274.             float heightDifference = Input.mouseScrollDelta.y < 0f ? cameraZoomPower : -cameraZoomPower;
    275.             cameraHeightTarget = currentCameraHeight + heightDifference;
    276.             cameraVerticalTarget = currentCameraVertical + heightDifference;
    277.             if (cameraHeightTarget > maxCameraHeight) cameraHeightTarget = maxCameraHeight;
    278.             else if (cameraHeightTarget < minCameraHeight) cameraHeightTarget = minCameraHeight;
    279.             if (cameraVerticalTarget > maxCameraVertical) cameraVerticalTarget = maxCameraVertical;
    280.             else if (cameraVerticalTarget < minCameraVertical) cameraVerticalTarget = minCameraVertical;
    281.         }
    282.  
    283.         private static readonly int moveSpeedModifier = Animator.StringToHash("MoveSpeedModifier");
    284.  
    285.         private void HandleCamera()
    286.         {
    287.             if (!cameraEnabled) return;
    288.             if (canRotateCamera)
    289.             {
    290.                 Vector3 eulerAngles = playerCamera.transform.rotation.eulerAngles;
    291.                 if (IsKeyHeld(useActionKeys
    292.                     ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(rotateLeftActionKeyName)
    293.                     : camRotateKeyLeft))
    294.                 {
    295.                     playerCamera.transform.rotation = Quaternion.Euler(eulerAngles.x,
    296.                         eulerAngles.y - cameraRotateSpeed * Time.deltaTime, eulerAngles.z);
    297.  
    298.                 }
    299.                 else if (IsKeyHeld(useActionKeys
    300.                     ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(rotateRightActionKeyName)
    301.                     : camRotateKeyRight))
    302.                 {
    303.                     playerCamera.transform.rotation = Quaternion.Euler(eulerAngles.x,
    304.                         eulerAngles.y + cameraRotateSpeed * Time.deltaTime, eulerAngles.z);
    305.                 }
    306.             }
    307.  
    308.             playerCamera.transform.position = (transform.position + Vector3.up * 0.8f) -
    309.                                               (playerCamera.transform.forward * currentCameraHeight);
    310.         }
    311.  
    312.  
    313.         private void LerpCameraHeight()
    314.         {
    315.             currentCameraHeight = Mathf.Lerp(currentCameraHeight, cameraHeightTarget, Time.deltaTime * cameraZoomSpeed);
    316.             currentCameraVertical =
    317.                 Mathf.Lerp(currentCameraVertical, cameraVerticalTarget, Time.deltaTime * cameraZoomSpeed);
    318.         }
    319.  
    320.         #endregion
    321.  
    322.         #region NAVIGATION
    323.  
    324.  
    325.         private void TriggerQueuedRPGBAction()
    326.         {
    327.             switch (currentQueuedRPGBAction.InteractableType)
    328.             {
    329.                 case RPGCombatDATA.INTERACTABLE_TYPE.None:
    330.                     break;
    331.                 case RPGCombatDATA.INTERACTABLE_TYPE.NeutralUnit:
    332.                     break;
    333.                 case RPGCombatDATA.INTERACTABLE_TYPE.EnemyUnit:
    334.                     break;
    335.                 case RPGCombatDATA.INTERACTABLE_TYPE.AlliedUnit:
    336.                 case RPGCombatDATA.INTERACTABLE_TYPE.InteractableObject:
    337.                 case RPGCombatDATA.INTERACTABLE_TYPE.CraftingStation:
    338.                 case RPGCombatDATA.INTERACTABLE_TYPE.LootBag:
    339.                 case RPGCombatDATA.INTERACTABLE_TYPE.WorldDroppedItem:
    340.                     currentQueuedRPGBAction.interactableREF.Interact();
    341.                     break;
    342.             }
    343.  
    344.             LookAtPos(currentQueuedRPGBAction.go.transform.position);
    345.             currentQueuedRPGBAction = null;
    346.         }
    347.  
    348.         private bool IsPathAllowed(Vector3 point)
    349.         {
    350.             NavMeshPath path = new NavMeshPath();
    351.             return NavMesh.CalculatePath(transform.position, point, NavMesh.AllAreas, path);
    352.         }
    353.  
    354.         private ValidCoordinate closestAllowedDestination(Vector3 point)
    355.         {
    356.             ValidCoordinate newResult = new ValidCoordinate();
    357.             if (!NavMesh.SamplePosition(point, out var hit, samplePositionDistanceMax, NavMesh.AllAreas))
    358.                 return newResult;
    359.             newResult.Valid = true;
    360.             newResult.ValidPoint = hit.position;
    361.             return newResult;
    362.         }
    363.  
    364.         private bool IsKeyDown(KeyCode key)
    365.         {
    366.             return Input.GetKeyDown(key);
    367.         }
    368.  
    369.         private bool IsKeyHeld(KeyCode key)
    370.         {
    371.             return Input.GetKey(key);
    372.         }
    373.  
    374.         private bool IsKeyUp(KeyCode key)
    375.         {
    376.             return Input.GetKeyUp(key);
    377.         }
    378.  
    379.         private MoveInputType MovingInput()
    380.         {
    381.             MoveInputType moveInputType = new MoveInputType();
    382.             if (IsKeyDown(useActionKeys
    383.                 ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(moveKeyActionKeyName)
    384.                 : moveKey))
    385.             {
    386.                 lastClick = Time.time;
    387.                 moveInputType.Valid = true;
    388.                 return moveInputType;
    389.             }
    390.  
    391.             if (!(Time.time >= nextHoldMove))
    392.             {
    393.                 moveInputType.Valid = false;
    394.                 return moveInputType;
    395.             }
    396.  
    397.             if (!(Time.time >= lastClick + timeAfterClickForHolding))
    398.             {
    399.                 moveInputType.Valid = false;
    400.                 return moveInputType;
    401.             }
    402.  
    403.             if (!allowHoldKey || !IsKeyHeld(useActionKeys
    404.                 ? RPGBuilderUtilities.GetCurrentKeyByActionKeyName(moveKeyActionKeyName)
    405.                 : moveKey)) return moveInputType;
    406.             nextHoldMove = Time.time + holdMoveCd;
    407.             moveInputType.Valid = true;
    408.             moveInputType.Held = true;
    409.             return moveInputType;
    410.         }
    411.  
    412.         private void TriggerNewDestination(Vector3 location)
    413.         {
    414.             agent.SetDestination(location);
    415.             StartCoroutine(SetCharacterState(CharacterState.Moving));
    416.         }
    417.  
    418.         private bool IsDestinationReached()
    419.         {
    420.             return !agent.hasPath || agent.remainingDistance <= (agent.stoppingDistance + destinationTreshold);
    421.         }
    422.  
    423.         private bool IsPathTooClose(Vector3 point)
    424.         {
    425.             return Vector3.Distance(transform.position, point) < minimumPathDistance;
    426.         }
    427.  
    428.         #endregion
    429.  
    430.         #region FEEDBACK
    431.  
    432.         private void SpawnGroundPathMarker(Vector3 point, bool rectified)
    433.         {
    434.             GameObject prefab = rectified ? validGroundPathPrefab : rectifiedGroundPathPrefab;
    435.             if (prefab == null) return;
    436.             GameObject marker = Instantiate(prefab,
    437.                 new Vector3(point.x + markerPositionOffset.x, point.y + markerPositionOffset.y,
    438.                     point.z + markerPositionOffset.z), prefab.transform.rotation);
    439.             Destroy(marker, groundMarkerDuration);
    440.         }
    441.  
    442.         private void PlayGroundPathAudio(bool rectified)
    443.         {
    444.             AudioClip audio = rectified ? validGroundPathAudio : rectifiedGroundPathAudio;
    445.             if (audio == null) return;
    446.             if (cameraAudio == null) InitAudioSource();
    447.             cameraAudio.PlayOneShot(audio);
    448.         }
    449.  
    450.         #endregion
    451.  
    452.         #region OTHER
    453.  
    454.         private void InitAudioSource()
    455.         {
    456.             AudioSource ASource = playerCamera.GetComponent<AudioSource>();
    457.             if (ASource == null)
    458.             {
    459.                 ASource = playerCamera.gameObject.AddComponent<AudioSource>();
    460.             }
    461.  
    462.             cameraAudio = ASource;
    463.         }
    464.  
    465.         public void StartAnimation(CharacterState state)
    466.         {
    467.             if (anim == null) return;
    468.             ResetStateAnimations();
    469.             switch (state)
    470.             {
    471.                 case CharacterState.Idle:
    472.                     break;
    473.                 case CharacterState.Moving:
    474.                     anim.SetBool(IsMoving, true);
    475.                     break;
    476.                 case CharacterState.Standing:
    477.                     anim.SetBool(Standing, true);
    478.                     break;
    479.             }
    480.         }
    481.  
    482.         private void ResetStateAnimations()
    483.         {
    484.             anim.SetBool(IsMoving, false);
    485.             anim.SetBool(Standing, false);
    486.         }
    487.  
    488.         #endregion
    489.  
    490.     }
    491. }