Input.GetAxisRaw always returns -0.6
Hello,
I'm having difficulties with a PlayerMovement script. The problem is that when I use Input.GetAxisRaw ("Vertical") it always returns -0.661099 even when I'm not pressing anything. The strange thing for me is that the same function with ("Horizontal") works fine.
It's the second time I have this problem and I don't know why. If anyone could help me, I would be really happy. Thanks :)
why do you only make the player move if its > 0.5 and < -0.5?
I watched a tutorial and he did that too. I'm a beginner but it didn't make sense to me either. But it's the same if its 0 ins$$anonymous$$d of 0.5f.
Tutorial: https://www.youtube.com/watch?v=Tm2L-_0eIeY∈dex=2&list=PLiyfvmtjWC_X6e0EYLPczO9tNCkm2dzkm
Answer by kalen_08 · Jul 20, 2018 at 08:48 PM
I checked mine out and its all good..
void Update ()
{
float xSpeed = Input.GetAxisRaw("Horizontal");
float ySpeed = Input.GetAxisRaw("Vertical");
Vector2 movement = new Vector2 (xSpeed, ySpeed) * moveSpeed;
transform.Translate(movement);
}
also since you're a beginner a piece of advice would be not to write the same thing so many times. If I were to do what you're doing I would do it this way.
[SerializeField] float speedMultiplier = 1;
float xSpeed = 0;
float ySpeed = 0;
// if you want to see these values, change the inspector to Debug mode
//you will then be able to see these..
void Update ()
{
xSpeed = Input.GetAxisRaw("Horizontal");
ySpeed = Input.GetAxisRaw("Vertical");
if (Mathf.Abs(xSpeed) > 0.5f)
{
transform.Translate(xSpeed, 0, 0);
}
if (Mathf.Abs(ySpeed) > 0.5f)
{
transform.Translate(0, ySpeed, 0);
}
}
This is more simple. You call
Input.GetAxisRaw("Horizontal");
3 times and same for vertical. also as in my example, unless you need to use the speed values somewhere else then just have them as local variables.
As you see in my example #2 I use
[SerializeField]
which means that you can change the value in the inspector but other classes can't access it. this is best. The most protected a variable can be the better... If you need anything else just let me know.
Your answer
Follow this Question
Related Questions
Face direction of a Vector 3 1 Answer
How to make an object go the direction it is facing? (Im new) 0 Answers
Movement Sticking 0 Answers
Why isn't my player moving down? 0 Answers
3rd Person Character Vertical Movement 0 Answers