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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Disabling player actions

Discussion in 'Scripting' started by alexxjaz, May 5, 2019.

  1. alexxjaz

    alexxjaz

    Joined:
    Sep 18, 2018
    Posts:
    45
    Hello, Im trying to make a Character Controller. This character can do things like moving, jumping, walljumping, dashing, attack etc...

    My problem is that i dont want that if he is jumping he can attack for example, or if he is dashing dont let him jump. I have several scripts with every action, one with dash, movement and jump, walljump, and i disable this scripts or enable it.

    I have tried to create a state machine but this gave me problems like moving and jumping at same time.


    There is a better way to do that? Because Im sure that this will give me problems later.

    PD: He doesnt start with all the abilities, unlocks them, i dont know if this matters.
     
  2. Grankor

    Grankor

    Joined:
    Mar 2, 2015
    Posts:
    14
    Lots of different approaches to this. :)

    One I've taken is using a bool in the character controller.
    This approach 'can' start to look like spaghetti, and can be difficult to manage if you have a lot of different states, but is pretty straight forward.

    Example below.
    In this case, you would want to call the ResetJump method once you detected a landing, or your animation completed (or however you're choosing to detect that)

    Code (CSharp):
    1.  
    2. private bool _isJumping = false;
    3.  
    4.  
    5. public void Jump()
    6. {
    7.     _isJumping = true;
    8. }
    9.  
    10. public void Attack()
    11. {
    12.    if(_isJumping)
    13.       return;
    14.  
    15.    //Attack script here
    16. }
    17.  
    18. public void ResetJump()
    19. {
    20.    _isJumping = false;
    21. }
    22.  
    23.  
     
  3. alexxjaz

    alexxjaz

    Joined:
    Sep 18, 2018
    Posts:
    45
    i like your way, im trying to use it right now :D Thx
     
    Grankor likes this.