Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Stop player to move when attacking

Discussion in 'Scripting' started by Paykoman, Feb 25, 2020.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im trying to stop my player to move/slide when i press the attack botton, so i want him to stop during attack animation, i try diferrent bools but i cant get it to work, any help plz? I trigger on a bool "attack" to use the attack animation.

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     Camera cam;
    7.  
    8.     public LayerMask movementMask;
    9.  
    10.     public LayerMask interactableMask;
    11.  
    12.     PlayerMotor motor;
    13.  
    14.     PlayerCombat combat;
    15.  
    16.     public Interactable focus;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         cam = Camera.main;
    22.         motor = GetComponent<PlayerMotor>();
    23.         combat = GetComponent<PlayerCombat>();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (EventSystem.current.IsPointerOverGameObject())
    30.             return;
    31.  
    32.         if (Input.GetMouseButton(0))
    33.         {
    34.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    35.  
    36.             RaycastHit hit;
    37.  
    38.             if (Physics.Raycast(ray, out hit, 100, movementMask))
    39.             {
    40.                 // Move the player
    41.                 motor.MoveToPoint(hit.point);
    42.  
    43.                 // Stop focusing any onject
    44.                 RemoveFocus();
    45.             }
    46.  
    47.             if (Physics.Raycast(ray, out hit, 100, interactableMask))
    48.             {
    49.                 Interactable interactable = hit.collider.GetComponent<Interactable>();
    50.  
    51.                 if (interactable != null)
    52.                 {
    53.                     SetFocus(interactable);
    54.                 }
    55.             }
    56.         }
    57.  
    58.         if (Input.GetKeyDown(KeyCode.Alpha1))
    59.         {
    60.             combat.Attack();
    61.         }
    62.  
    63.         void SetFocus(Interactable newFocus)
    64.         {
    65.             if(newFocus != focus)
    66.             {
    67.                 if (focus != null)
    68.                     focus.DeFocused();
    69.  
    70.                 focus = newFocus;
    71.                 motor.FollowTarget(newFocus);
    72.             }
    73.  
    74.             newFocus.OnFocused(transform);
    75.                        
    76.          }
    77.  
    78.         void RemoveFocus()
    79.         {
    80.             if (focus != null)
    81.                 focus.DeFocused();
    82.  
    83.             focus = null;
    84.             motor.StopFollow();
    85.         }
    86.     }
    87. }
     
    TimmyTheTerrible likes this.
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You only detect the input for attacking after you already moved the object, so how would you stop moving based on that information? If you dont want any movement while combating, you could detect Alpha1, attack if detected, and put your entire movement code into the else statement. Either way, you need to detect whether you are attacking or not before you move, in order to be able to prevent movement based on this information.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I try it but from somekind of fault or it always getting true or false, stopping the attack animation or dont even move...