Forum rules - please read before posting.

How to retain jumping direction when button not held?

I'm pretty new to AC and working my way through the tutorials. I was pleasantly surprised at how good the smooth first-person controller was after I tweaked the values to my liking. There is one thing that irks me however and that is the fact that a direction key must be held to carry directional momentum through a jump. For example, if you let go of the forward key mid-jump, you will stop in the air and fall straight down. This is unusual when compared to how jumping works in most games and it's been tripping me up a bit. Does anyone know the best way to fix this?

Comments

  • edited December 2022

    Welcome to the community, @spookyrockjaw.

    You can have the Player maintain their direction when in mid-air by using a custom script to override the returned input values for the Horizontal and Vertical axes.

    Details on how this topic works can be found in the Manual's "Remapping inputs" chapter, but try attaching this script (LockJumpDirection.cs) to your Player character:

    using UnityEngine;
    using AC;
    
    public class LockJumpDirection : MonoBehaviour
    {
    
        private float lastGroundedHorizontalValue;
        private float lastGroundedVerticalValue;
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetAxisDelegate = MyGetAxis;
        }
    
        private float MyGetAxis (string axisName)
        {
            try
            {
                float inputValue = Input.GetAxis (axisName);
    
                if (KickStarter.stateHandler.IsInGameplay ())
                {
                    if (axisName == "Horizontal")
                    {
                        if (KickStarter.player.IsGrounded ())
                        {
                            lastGroundedHorizontalValue = inputValue;
                        }
                        else
                        {
                            return lastGroundedHorizontalValue;
                        }
                    }
                    else if (axisName == "Vertical")
                    {
                        if (KickStarter.player.IsGrounded ())
                        {
                            lastGroundedVerticalValue = inputValue;
                        }
                        else
                        {
                            return lastGroundedVerticalValue;
                        }
                    }
                }
    
                return inputValue;
            }
            catch
            {
                return 0f;
            }
        }
    
    }
    
  • Hi Chris. Thank you for your prompt reply and thank you for the excellent asset. I am already getting excited by the possibilities.

    I tried the attached script and it had the desired effect while jumping however it has caused a detrimental effect to the grounded player movement. At first, it seemed to me that there was less friction because I was sliding more but after toying with the deceleration value I have found that it's not really the friction but there is a noticeable delay from when I release the movement key to when the player stops. It's like the movement keys are sticky now.

    I don't want to take up too much of your time. I can always investigate this down the line... but if you have any suggestions please let me know. Thanks!

  • Try replacing the script's:

    float inputValue = Input.GetAxis (axisName);
    

    line with:

    float inputValue = Input.GetAxisRaw (axisName);
    

    That'll cause it to ignore the Sensitivity / Gravity values for your inputs. Does that make things more responsive?

    If not, the issue may be related to how AC determines if the Player is "grounded" or not - but this depends on what components you have attached. Can you share a screenshot showing your Player's full Inspector? If they have a Rigidbody + Capsule Collider combination, try replacing them with a Character Controller.

  • I've been out of town but I've just tried it and yes, that seems to have fixed the issue. Thanks!

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.