(Newbie's First Project) Object doesn't stop moving until long after I let go of the button
Hi! I have very limited coding experience, and I've been watching/following some tutorials to get myself familiar with the program.
I started working on my first project, and I got the player movement script to work more or less the way I want it to, using only horizontal movement so far. The problem is that when I let go of the left/right input, the object continues to move at full speed for an unwanted amount of time before it starts to slow down. The length of the delay increases with the speed of the object. Increasing 'hDecel' increases how quickly it decelerates, but it doesn't change this delay.
I'm sure I'm doing something wrong, but I can't figure out what it is exactly. Any help would be greatly appreciated! Also, if you see any inefficiencies in my code, feel free to let me know- I want to get better!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// Defines variables for the ship's momentum
private float vMomentum = 0;
private float hMomentum = 0;
// Defines global variables for the ship's top speed, acceleration, and deceleration
public float vTopSpeed = 1;
public float hTopSpeed = 1;
public float vDecel = 1;
public float hDecel = 1;
public float vAccel = 1;
public float hAccel = 1;
// Update is called once per frame
void Update ()
{
// Moves the ship to the RIGHT when the player is holding RIGHT
if (Input.GetAxis("Horizontal") > 0)
{
if (hMomentum < hTopSpeed)
hMomentum += hAccel * Time.deltaTime;
if (hMomentum > hTopSpeed)
hMomentum = hTopSpeed;
}
// Moves the ship to the LEFT when the player is holding LEFT
if (Input.GetAxis("Horizontal") < 0)
{
if (hMomentum > -hTopSpeed)
hMomentum -= hAccel * Time.deltaTime;
if (hMomentum < -hTopSpeed)
hMomentum = -hTopSpeed;
}
// Slows the ship down if the player is holding neither LEFT nor RIGHT
if (Input.GetAxis("Horizontal") == 0)
{
if (hMomentum > 0)
hMomentum -= hDecel * Time.deltaTime;
if (hMomentum < 0)
hMomentum += hDecel * Time.deltaTime;
// Prevents the ship from being stuck at a very low momentum
if (hMomentum < hDecel * Time.deltaTime)
if (hMomentum > -hDecel * Time.deltaTime)
hMomentum = 0;
}
// Follows through with movement, assuming there is any movement to be done
if (hMomentum != 0 || vMomentum != 0)
transform.Translate(hMomentum, vMomentum, 0);
}
}
EDIT: Okay, I figured out what the problem is, but I'm still not sure how to fix it. Apparently when you let go of left/right, it doesn't immediately set 'Input.GetAxis("Horizontal"))' to 0- it gradually decreases back to 0. Is there some function I can put there ins$$anonymous$$d that will return whether or not the player is pressing either left or right?
Answer by Backside2357 · Jan 20, 2018 at 09:13 PM
Add this line at the bottom of your Update function:
Debug.Log(Input.GetAxis("Horizontal").ToString() + " | " + hMomentum.ToString());
When you take a look at the Unity console, you will see that the problem is the behavior of the Input.GetAxis function: It's not just 1, -1 or 0, but something inbetween -1 and 1. This means that if you stop pressing the key, the value that is returned is not immediately 0, because there is some interpolation over time. Not sure if this can be disabled though.
Btw: Please try to always use the curly brackets after an if statement:
if(true)
{
//do stuff
}
I know this is "annoying work" but it makes the code easier to read and prevents you from making accidental mistakes.
Thanks for the tip about the brackets. =) I'll do that now.
I just found out that the "returning to 0 over time" thing can be altered in the 'Input' manager of the Project Settings. Setting 'Gravity' to 100 causes the axis to reset to 0 immediately. Thank you for the help- it's good to know I was on the right track!