Forum rules - please read before posting.

Disable player movement via script (Direct Controls)

Hello,
I have a script that casts a ray to detect if the player is trying to walk through a wall.

Once a wall is detected, I want to disable the player input (something similar to ManageSystem\Input\Disabled), but I can't find it on the api.

Once the player choses a different direction or releases the stick/arrow, the ray retracts (doesn't intersect with wall/obstacle anymore) and I want to enable the input again.

Or is there a better method?

Thanks

Comments

  • This is for a 3D game?

    If you're using Mecanim for the character's animation engine, the Player's Inspector has a Slow movement near walls? option that should do something very similar already.

    As the character approaches a collider on a given layer, they'll slow down according to how close they are. This is to prevent them from playing walk/run animations when moving into a wall.

    If you wanted to incorporate your own such method, disable the above and have your script to lower the player's movement speed to something close to (but not exactly) zero:

    AC.KickStarter.player.walkSpeedScale = 0.01f;
    

    This is safer than disabling input altogether, which may prevent the Player from being able to move away from a wall once they get too close.

    For completeness, though, the code-equivalent of disabling input through the Engine: Manage systems Action is:

    KickStarter.stateHandler.SetInputSystem (false);
    

    And for movement:

    KickStarter.stateHandler.SetMovementSystem (false);
    
  • Hey Chris,
    This is for a 3d game but I am using a 2d character (Sprites Unity).

    You are right, disabling inputs blocks the character.

    Another issue, using this method, is that even though the character stops moving, the sprite keeps playing the walk animation.

    Seems like this issue will require some more complex system?

  • If you're using Sprites Unity, then the walk speed is separate to your animation, because your Animator is told to play walk/idle animations directly.

    If you switch to Sprites Unity Complex, though, then you can rely on a "Move speed" Float parameter in your Animator to control which animation is played. This mode also has the "Slow movement near walls?" feature.

    In that case, all you'd need to do is set the walk speed to be less than the threshold for the Idle -> Walk transition. For example, if the transition required the "Move speed" Float to be greated than 0.1, you could set it to 0.05 to prevent it from triggering.

    See the Manual's "Character animation (Sprites Unity Complex)" chapter for details on setting up 2D characters with this engine.

    While that's the "best practice" method, you might alternatively be able to get by with Sprites Unity by changing the name of the walk animation to match the idle animation. That way, when the character is moving into a wall, he'll play the Idle animation thinking that that's the Walk animation:

    AC.KickStarter.player.walkAnimSprite = AC.KickStarter.player.idleAnimSprite;
    
  • edited December 2020

    Thanks Chris,
    I have tried using the "Slow movement near walls?" feature in a blank AC project, using the "Brain2D_SpritesUnityComplex" but it seems like my setup is not working.

    • Direct Controls;
    • 3d Game;
    • Removed 2d rigid body and 2d circle collider from the player;
    • Added 3d rigid body and sphere collider to the player;
    • Created "Collisions" layer and set the wall object to use it;
    • Specified "Collisions" as wall layer on the player prefab;
    • Toggled "Slow movement near walls?" feature on player;
    • Added box collider to wall;

    Is this feature meant to be used in a 3d project at all?

    Using Unity 2018.4.0f1 and AC 1.72.4.

    Interestingly enough, I have checked inside the Player.cs script and could not find any reference to such feature?

    Thanks!

  • Yes, it should work in both 2D and 3D scenes.

    Use a Capsule Collider instead of a Sphere Collider - even if it ends up being the same shape, AC uses it to calculate the necessary Raycast.

    The other settings such as "Collider distance" also factor in - you may need to play around with things at runtime to get the right behaviour based on your scale.

    I have checked inside the Player.cs script and could not find any reference to such feature?

    The calculation is made in the Char script's UpdateWallReductionFactor method.

  • Thanks Chris,
    Using the capsule collider instead of the sphere did the trick!

    And thanks for replying on the 25th of December!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.