Forum rules - please read before posting.

Changing Player Speed at Runtime with Animator Parameter?

I implemented a crouch animation into the downloadable FirstPersonPlayer from the AC website. Looks really good I think. But I am a bit stumped when it comes to changing the player's movement speed when crouched for running and walking. I'm using Bolt visual scripting, but that shouldn't matter. The issue is I'm unsure where to access the movement speed variables in the AC scripts. If it were a simple SerializedField in the Player script it wouldn't be a problem, but I can't find where to access it. I'm hoping to have the Animator "IsCrouching" bool parameter I made affect the movement speed at runtime, and toggle between two preset values.

Comments

  • The Player's regular movement speed is determined by their walkSpeedScale property. This is exposed as the "Walk speed scale" value in their Inspector.

    If the Animator is attached to the character's root, you should be able to incorporate this value into your animation, i.e. animate it at a reduced value as part of a "crouching walk" animation.

    Otherwise, you can attach a simple script that reads the value of the IsCrouching bool parameter, and updates the value accordingly:

    using UnityEngine;
    using AC;
    
    public class CrouchSpeedControl : MonoBehaviour
    {
    
        public float crouchSpeed = 1f;
        public float normalSpeed = 2f;
        public Animator _animator;
        public Player _player;
    
        void Update ()
        {
            bool isCrouching = _animator.GetBool ("IsCrouching");
            _player.walkSpeedScale = isCrouching ? crouchSpeed : normalSpeed;
        }
    
    }
    
  • WOW! I thought that things I couldn't right click to keyframe couldn't be animated, but apparently I can just add these variables as properties in the animation clip and change it! This changes my life, thanks! Lol

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.