Forum rules - please read before posting.

Changing the first person walk speed and preventing running when entering a trigger.

edited February 6 in Technical Q&A

Hello, I'm trying to reduce the player's walking speed and disable running when entering water, similar to how the Crouch script works, but using a trigger instead of a button. Is there a way to do this? Thanks in advance.

Comments

  • edited February 6

    Welcome to the community, @Rantceck.

    You can use a pair of Triggers (Enter and Exit) to run Character: Animate and Player: Constrain Actions to adjust the movement speed and prevent/allow running.

    Alternatively, you can rely on a custom script, attached to a Collider with "Is Trigger" checked, to handle this:

    using UnityEngine;
    using AC;
    
    public class PlayerWater : MonoBehaviour
    {
    
        [Range (0f, 1f)] public float speedReduction = 0.5f;
    
        void OnTriggerEnter (Collider collider)
        {
            if (KickStarter.player == null || KickStarter.player.gameObject != collider.gameObject) return;
    
            player.walkSpeedScale *= speedReduction;
            player.runningLocked = PlayerMoveLock.AlwaysWalk;
        }
    
        void OnTriggerExit (Collider collider)
        {
            if (KickStarter.player == null || KickStarter.player.gameObject != collider.gameObject) return;
    
            player.walkSpeedScale /= speedReduction;
            player.runningLocked = PlayerMoveLock.Free;
        }
    
    }
    
  • Thank you, it took me some time to realize my player prefab was broken some how and didn't do anything when I ran "Character: Animate and Player: Constrain", tested both options with a clean prefab and worked perfectly.

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.