(HELP)Character is not moving back smoothly when finishing an action(Snaps back to value).

I’ve been using Unity for four years, and I’m making a small Sonic project. One of the issues I’m having is that whenever I make Sonic perform an action that requires him to stay still, such as crouching, he instantly shoots back into his full speed when I release the button while still holding on to a input value. For example, I made my own stomp, and when he lands from it, he instantly goes back to full on dashing when holding the forward key. He gradually gains speed just like the games when I’m not performing any actions, but as soon as I need him to be stationary after the action is finished, that’s when the problem occurs. I’ve tried two methods: setting the input values to zero (didn’t work) and using vector3.zero (which also didn’t work). I am very dedicated to this project, so if anyone can help me out, I would greatly appreciate it. I’ve provided an example below from Sonic Generations. When he stomps and starts gradually moving again, he doesn’t instantly shoot back running again. I am also using a Character Controller Component.

i used a small little cube movement code to demonstrate

private CharacterController CC;
public bool Attacking;
private Input_Movement Input_Movement;
// Start is called before the first frame update
void Start()
{
CC = GetComponent();
Input_Movement = GetComponent<Input_Movement>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButton(“Jump”))
{

Input_Movement.XInput = 0;
Input_Movement.YInput = 0;
}
if (!Input.GetButton(“Jump”))
{

}
Input_Movement.input = new Vector3(Input_Movement.XInput, 0, Input_Movement.YInput) * 4;
CC.Move(Input_Movement.input * Time.deltaTime);
}

Smoothing movement between any two particular values:

https://discussions.unity.com/t/812925/5

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

Almost nobody bothers to read unformatted code. If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

1 Like

Thanks for replying, I will try this method and will let you know how it works out

I’m still having trouble here’s my actual code for the stomp and the movement.

Velocity = Vector3.Lerp(Velocity, transform.forward * GetAnimSpeed * SpeedMult, Accel * Time.deltaTime); // Applies Movement
        anim.SetFloat(("Forward"), input.move.magnitude, AnimationSpeedTrans, Time.deltaTime);// Giving the player moving animation speed value based on the players input


        GetAnimSpeed = anim.GetFloat("Forward"); // The Main Animation Speed Value Return based on how fast you're running

        if (Stomping == true)
        {
            SlamSpeed = RealSlamSpeed;
          

            GetAnimSpeed = 0;
          
            Velocity = Vector3.zero;

        }
        if (Stomping == false)
        {
            SlamSpeed = 0;
        }

He halts in the air like i want him to but as soon as he lands he goes back to running full speed while holding the movement keys. When i tried making the input vector2.zero it partially works when he lands he’s not moving again until you let go and press the movement keys again

This just sounds like you have a bug… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

I would also HIGHLY recommend you break apart crazy code lines like these:

Otherwise who knows what is going on there. Get it into local variables so you can print them out and reason about why they are wrong and then fix them.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

1 Like