Search Unity

Resolved Enabling and Disabling Movement in Third Person Game

Discussion in 'Input System' started by Draugrlord, Jan 16, 2023.

  1. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    30
    I am creating a third person game and I have my code set up so that when my character attacks, it stops the player from moving. However, I am not able to get it to allow the player to move once the attack has finished.

    From Code line 107 down is where I have my attack and animation triggering and the movement being disabled which works. Soon as my character triggers his attack animation he stops with no issues. I just can't get the input for movement to enable again.

    I feel the fix is a simple coding fix and I'm just not able to see it. Any help is greatly appreciated.

    EDIT: I was finally able to figure this out and for anyone that comes across my post while also trying to figure this out here is the solution I came across which is to use animation events. I set up to methods in my code to enable and disable movement then set them as the function in the animation events for my attacking animation and now my character will stop moving when attacking and then will be able to move again once the animation has finished. I will update the code to show how I wrote my two new methods. The methods can be found at the bottom of the script after my attack animation.

    Here is my updated code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private ThirdPersonActions inputActions;
    7.     private InputAction move;
    8.  
    9.     private Rigidbody rb;
    10.     [SerializeField]
    11.     private float movementForce = 1f;
    12.     [SerializeField]
    13.     private float jumpForce = 3f;
    14.     [SerializeField]
    15.     private float maxSpeed = 5f;
    16.  
    17.     private Vector3 forceDirection = Vector3.zero;
    18.  
    19.     [SerializeField]
    20.     private Camera playerCamera;
    21.     private Animator anim;
    22.  
    23.     private void Awake()
    24.     {
    25.         rb = this.GetComponent<Rigidbody>();
    26.         inputActions = new ThirdPersonActions();
    27.         anim = this.GetComponent<Animator>();
    28.     }
    29.  
    30.     private void OnEnable()
    31.     {
    32.         inputActions.Player.Jump.started += DoJump;
    33.         inputActions.Player.Fire.started += DoAttack;
    34.         move = (inputActions.Player.Move);
    35.         inputActions.Player.Enable();
    36.     }
    37.  
    38.  
    39.     private void OnDisable()
    40.     {
    41.         inputActions.Player.Jump.started -= DoJump;
    42.         inputActions.Player.Fire.started -= DoAttack;
    43.         inputActions.Player.Disable();
    44.     }
    45.  
    46.     private void FixedUpdate()
    47.     {
    48.         forceDirection += move.ReadValue<Vector2>().x * movementForce * GetCameraRight(playerCamera);
    49.         forceDirection += move.ReadValue<Vector2>().y * movementForce * GetCameraForward(playerCamera);
    50.  
    51.         rb.AddForce(forceDirection, ForceMode.Impulse);
    52.         forceDirection = Vector3.zero;
    53.  
    54.         if (rb.velocity.y < 0f)
    55.             rb.velocity -= Physics.gravity.y * Time.fixedDeltaTime * Vector3.down;
    56.  
    57.         Vector3 horizontalVelocity = rb.velocity;
    58.         horizontalVelocity.y = 0f;
    59.         if (horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
    60.             rb.velocity = horizontalVelocity.normalized * maxSpeed + Vector3.up * rb.velocity.y;
    61.  
    62.         LookAt();
    63.     }
    64.  
    65.     private void LookAt()
    66.     {
    67.         Vector3 direction = rb.velocity;
    68.         direction.y = 0f;
    69.  
    70.         if (move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    71.             this.rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
    72.         else
    73.             rb.angularVelocity = Vector3.zero;
    74.     }
    75.  
    76.     private Vector3 GetCameraForward(Camera playerCamera)
    77.     {
    78.         Vector3 forward = playerCamera.transform.forward;
    79.         forward.y = 0;
    80.         return forward.normalized;
    81.     }
    82.  
    83.     private Vector3 GetCameraRight(Camera playerCamera)
    84.     {
    85.         Vector3 right = playerCamera.transform.right;
    86.         right.y = 0;
    87.         return right.normalized;
    88.     }
    89.  
    90.     private void DoJump(InputAction.CallbackContext obj)
    91.     {
    92.         if (IsGrounded())
    93.         {
    94.             forceDirection += Vector3.up * jumpForce;
    95.         }
    96.     }
    97.  
    98.     private bool IsGrounded()
    99.     {
    100.         Ray ray = new(this.transform.position + Vector3.up * 0.25f, Vector3.down);
    101.         if (Physics.Raycast(ray, out _, 0.3f))
    102.             return true;
    103.         else
    104.             return false;
    105.     }
    106.  
    107.     private void DoAttack(InputAction.CallbackContext obj)
    108.     {      
    109.         if (IsAttacking())
    110.         {
    111.             anim.SetTrigger("Attack");          
    112.         }
    113.     }    
    114.  
    115.     private bool IsAttacking()
    116.     {
    117.         if (Input.GetMouseButton(0))
    118.             return true;
    119.         else
    120.             return false;
    121.     }
    122.  
    123.     //Stops movement when attacking
    124.     private void DisableMove()
    125.     {
    126.         inputActions.Player.Move.Disable();
    127.     }
    128.  
    129.     private void EnableMove()
    130.     {
    131.         inputActions.Player.Move.Enable();
    132.     }
    133. }
     
    Last edited: Jan 23, 2023