Forum rules - please read before posting.

How to add a sprint toggle?

I'm trying to make my character sprint when I press the left shift button. I already have an animation for walking and sprinting but I don't know where to add the keypress/toggle.

Comments

  • edited September 2020

    Well, I've got my left shift to activate my animation (set as a trigger) and the walking speed to increase to running, but the run takes holding down keypresses while the animation is just a toggle. So either I'd have to set the sprint to toggle instead of holding down shift (which I can't figure out how to do) or somehow make the animation script detect when I stop holding down the shift key.

    Here's my script code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class sprint : MonoBehaviour
    {
        Animator anim;
        // Start is called before the first frame update
        void Start()
        {
            anim = GetComponent<Animator>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.LeftShift)) anim.SetTrigger("Run");
        }
    

    Whether I use GetyKeyDown or just GetKey, neither detects releasing and holding down the shift key, both act as a toggle. Any ideas?

  • Welcome to the community, @BiZark.

    Your script doesn't mention AC, but with that method I would expect you'd want to control a Boolean animator parameter, rather than a Trigger, since a Trigger can only fire once. You can have this Bool value toggle between true/false each time an input is pressed.

    However, a character that relies on AC for motion shouldn't need such a script. If you're Movement method is set to Direct, you can toggle running by pressing an input named "RunToggle".

    See the Settings Manager's "Available inputs" panel for a complete list of inputs you can make use of.

  • The RunToggle is good, but how would I activate an animation without the use of my own script? And what would the code look like for a Bool?

    Thanks!

  • how would I activate an animation without the use of my own script?

    If the AC player is running, they'll play their "run" animation automatically, provided you've supplied one. How exactly this is done depends on your character's chosen "Animation engine". What is yours set to? Let's see some screenshots of your Player's Inspector and Animator Controller for context.

    And what would the code look like for a Bool?

    Something like:

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        bool value = anim.GetBool ("Run");
        anim.SetBool ("Run", !value);
    }
    
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.