Home > Software engineering >  Unity - I have my movement script with a hold down run but i want to turn it into a toggle run
Unity - I have my movement script with a hold down run but i want to turn it into a toggle run

Time:05-28

AH Yes, I have this script for simple movement it has the main movement at the last. in the script i check for the input from my input script and do the movement and for script i check if the character is running (which is input from different script) and if it is running movement speed is equal to running speed. else it is walking speed. i want my character to have a toggle sprint instead of hold down (which this script has) Can anyone help me to input toggle run.Thank You

movement script

using System.Linq;
using UnityEngine;

namespace LowPolyShooterPack
{
    [RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
    public class Movement : MovementBehaviour
    {
    #region FIELDS SERIALIZED

    [Header("Audio Clips")]

    [Tooltip("The audio clip that is played while walking.")]
    [SerializeField]
    private AudioClip audioClipWalking;

    [Tooltip("The audio clip that is played while running.")]
    [SerializeField]
    private AudioClip audioClipRunning;

    [Header("Speeds")]

    [SerializeField]
    private float speedWalking = 5.0f;

    [Tooltip("How fast the player moves while running."), SerializeField]
    private float speedRunning = 9.0f;

    #endregion

    #region PROPERTIES

    //Velocity.
    private Vector3 Velocity
    {
        //Getter.
        get => rigidBody.velocity;
        //Setter.
        set => rigidBody.velocity = value;
    }

    #endregion

    #region FIELDS

    /// <summary>
    /// Attached Rigidbody.
    /// </summary>
    private Rigidbody rigidBody;
    /// <summary>
    /// Attached CapsuleCollider.
    /// </summary>
    private CapsuleCollider capsule;
    /// <summary>
    /// Attached AudioSource.
    /// </summary>
    private AudioSource audioSource;

    /// <summary>
    /// True if the character is currently grounded.
    /// </summary>
    private bool grounded;

    /// <summary>
    /// Player Character.
    /// </summary>
    private CharacterBehaviour playerCharacter;
    /// <summary>
    /// The player character's equipped weapon.
    /// </summary>
    private WeaponBehaviour equippedWeapon;

    /// <summary>
    /// Array of RaycastHits used for ground checking.
    /// </summary>
    private readonly RaycastHit[] groundHits = new RaycastHit[8];

    #endregion

    #region UNITY FUNCTIONS

    /// <summary>
    /// Awake.
    /// </summary>
    protected override void Awake()
    {
        //Get Player Character.
        playerCharacter = ServiceLocator.Current.Get<IGameModeService>().GetPlayerCharacter();
    }

    /// Initializes the FpsController on start.
    protected override  void Start()
    {
        //Rigidbody Setup.
        rigidBody = GetComponent<Rigidbody>();
        rigidBody.constraints = RigidbodyConstraints.FreezeRotation;
        //Cache the CapsuleCollider.
        capsule = GetComponent<CapsuleCollider>();

        //Audio Source Setup.
        audioSource = GetComponent<AudioSource>();
        audioSource.clip = audioClipWalking;
        audioSource.loop = true;
    }

    /// Checks if the character is on the ground.
    private void OnCollisionStay()
    {
        //Bounds.
        Bounds bounds = capsule.bounds;
        //Extents.
        Vector3 extents = bounds.extents;
        //Radius.
        float radius = extents.x - 0.01f;
        
        //Cast. This checks whether there is indeed ground, or not.
        Physics.SphereCastNonAlloc(bounds.center, radius, Vector3.down,
            groundHits, extents.y - radius * 0.5f, ~0, QueryTriggerInteraction.Ignore);
        
        //We can ignore the rest if we don't have any proper hits.
        if (!groundHits.Any(hit => hit.collider != null && hit.collider != capsule)) 
            return;
        
        //Store RaycastHits.
        for (var i = 0; i < groundHits.Length; i  )
            groundHits[i] = new RaycastHit();

        //Set grounded. Now we know for sure that we're grounded.
        grounded = true;
    }
        
    protected override void FixedUpdate()
    {
        //Move.
        MoveCharacter();
        
        //Unground.
        grounded = false;
    }

    /// Moves the camera to the character, processes jumping and plays sounds every 
        frame.
    protected override  void Update()
    {
        //Get the equipped weapon!
        equippedWeapon = playerCharacter.GetInventory().GetEquipped();
        
        //Play Sounds!
        PlayFootstepSounds();
    }

    #endregion

    #region METHODS

    private void MoveCharacter()
    {
        #region Calculate Movement Velocity

        //Get Movement Input!
        Vector2 frameInput = playerCharacter.GetInputMovement();
        //Calculate local-space direction by using the player's input.
        var movement = new Vector3(frameInput.x, 0.0f, frameInput.y);
        
        //Running speed calculation.
        if(playerCharacter.IsRunning())
            movement *= speedRunning;
        else
        {
            //Multiply by the normal walking speed.
            movement *= speedWalking;
        }

        //World space velocity calculation. This allows us to add it to the rigidbody's 
            velocity properly.
        movement = transform.TransformDirection(movement);

        #endregion
        
        //Update Velocity.
        Velocity = new Vector3(movement.x, 0.0f, movement.z);
    }

    /// <summary>
    /// Plays Footstep Sounds. This code is slightly old, so may not be great, but it 
        functions alright-y!
    /// </summary>
    private void PlayFootstepSounds()
    {
        //Check if we're moving on the ground. We don't need footsteps in the air.
        if (grounded && rigidBody.velocity.sqrMagnitude > 0.1f)
        {
            //Select the correct audio clip to play.
            audioSource.clip = playerCharacter.IsRunning() ? audioClipRunning : 
                audioClipWalking;
            //Play it!
            if (!audioSource.isPlaying)
                audioSource.Play();
        }
        //Pause it if we're doing something like flying, or not moving!
        else if (audioSource.isPlaying)
            audioSource.Pause();
    }

    #endregion
    }
}

Character Behavior

using UnityEngine;

namespace LowPolyShooterPack
{
/// <summary>
/// Character Abstract Behaviour.
/// </summary>
public abstract class CharacterBehaviour : MonoBehaviour
{
    #region UNITY

    /// <summary>
    /// Awake.
    /// </summary>
    protected virtual void Awake(){}

    /// <summary>
    /// Start.
    /// </summary>
    protected virtual void Start(){}

    /// <summary>
    /// Update.
    /// </summary>
    protected virtual void Update(){}

    /// <summary>
    /// Late Update.
    /// </summary>
    protected virtual void LateUpdate(){}

    #endregion
    
    #region GETTERS

    /// <summary>
    /// Returns the player character's main camera.
    /// </summary>
    public abstract Camera GetCameraWorld();
    
    /// <summary>
    /// Returns a reference to the Inventory component.
    /// </summary>
    public abstract InventoryBehaviour GetInventory();

    /// <summary>
    /// Returns true if the Crosshair should be visible.
    /// </summary>
    public abstract bool IsCrosshairVisible();
    /// <summary>
    /// Returns true if the character is running.
    /// </summary>
    public abstract bool IsRunning();
    
    /// <summary>
    /// Returns true if the character is aiming.
    /// </summary>
    public abstract bool IsAiming();
    /// <summary>
    /// Returns true if the game cursor is locked.
    /// </summary>
    public abstract bool IsCursorLocked();

    /// <summary>
    /// Returns true if the tutorial text should be visible on the screen.
    /// </summary>
    public abstract bool IsTutorialTextVisible();

    /// <summary>
    /// Returns the Movement Input.
    /// </summary>
    public abstract Vector2 GetInputMovement();
    /// <summary>
    /// Returns the Look Input.
    /// </summary>
    public abstract Vector2 GetInputLook();
    
    #endregion

    #region ANIMATION

    /// <summary>
    /// Ejects a casing from the equipped weapon.
    /// </summary>
    public abstract void EjectCasing();
    /// <summary>
    /// Fills the character's equipped weapon's ammunition by a certain amount, or fully if set to -1.
    /// </summary>
    public abstract void FillAmmunition(int amount);

    /// <summary>
    /// Sets the equipped weapon's magazine to be active or inactive!
    /// </summary>
    public abstract void SetActiveMagazine(int active);
    
    /// <summary>
    /// Reload Animation Ended.
    /// </summary>
    public abstract void AnimationEndedReload();

    /// <summary>
    /// Inspect Animation Ended.
    /// </summary>
    public abstract void AnimationEndedInspect();
    /// <summary>
    /// Holster Animation Ended.
    /// </summary>
    public abstract void AnimationEndedHolster();

    #endregion
    }
}

any help would be really grateful. BTW i tried myself by implementing this code'

if(Input.getKeyDown("shift") {
    playerCharacter.isRunning = !playerCharacter.isRunning;
}

if(playerCharacter.isRunning)
{
            movement *= speedRunning;
}

CodePudding user response:

I would guess all you gotta do is to add

Input.GetKeyDown(KeyCode.LeftShift) {
    playerCharacter.isRunning = !playerCharacter.isRunning;
}

Into your Update Function just before the MoveCharacter.

Maybe add a Debug.Log("Running: " playerCharacter.isRunning); in the if or in the Update Method.

I guess why the thing you tried does not work is because you would need to use Input.GetKeyDown("left shift") or Input.GetKeyDown("right shift") Using the KeyCode is more save in general though.

CodePudding user response:

if(Input.GetKeyDown(KeyCode.LeftShift)) 
        {
            playerCharacter.IsRunning(!playerCharacter.IsRunning);
        }

CodePudding user response:

I am using the new input for Input

CodePudding user response:

Ummm sorry I got it I just edited the input setting and set the intersection from none to a tap

  • Related