- Home /
I am having problems with making my character face the movement direction after stopping.
Ok so I'm making a 2D top down shooter game following a tutorial by Couch Ferret. When I press only press one direction, the character stays facing that direction when I let go. However when I press two buttons at once, like right and up, to move diagonally and then let go, the character faces one of the two directions but not diagonally. I guess this is because I am not letting go of both directions at exactly the same time but I still want the character to face diagonally even if I don't let go of the buttons at exactly the same time (maybe like a 0.5 second window). But I don't know how to do do it as I am still new to coding. Anyways here is my code:
public Rigidbody2D rb;
public float speed;
public Animator animator;
public Vector2 movement;
void Update()
{
movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
rb.velocity = Vector2.ClampMagnitude(movement, 1.0f) * speed;
if (movement != Vector2.zero)
{
animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
}
}
Thanks in advance for anyone who helps.
Answer by Chaos-Fusion · Jul 08, 2020 at 02:07 PM
IIRC, The reason the direction is not preserved is because of how the GetAxis works.
When you release one of the buttons, the value that represents that axis returns to zero. If you release 2 buttons, the last axis to equal to zero will be the direction the character is facing if that makes sense. When I worked on it, I had to create a variable that stored the values of the axis so I could preserve the direction. How you implement it will depend on what your script but this is something that I came up with though untested.
private float xSaved, ySaved;
void Update()
{
//By getting the raw inputs, it only returns a 1, 0 or -1 depend on the axis direction
float xRaw = Input.GetAxisRaw("Horizontal");
float yRaw = Input.GetAxisRaw("Vertical");
//Check if no input is being held.
if (xRaw!=0 || yRaw!=0)
{
//Get the direction from the regular axis.
xSaved = Input.GetAxis("Horizontal");
ySaved = Input.GetAxis("Vertical");
//Apply the preserved direction to your movement code
movement = new Vector2(xSaved, ySaved);
//rest of your code below here
}
}
I don't see how this can help. Like the variable movement still has the same values as if I had never used this bit of code in the first place. Like xSaved and ySaved are still just equal to Input.GetAxis("Horizontal/Vertical") which is what my code above already does. $$anonymous$$aybe I'm just not implementing it properly though.
Hmm, playing around with your code, I got it running however I also realised why my code suggestion wouldn't work properly in a different way. The characters accelleration and Decelleration are based on the GetAxis Value. Using the IF statement causes the characters direction to be saved however the character will continue moving in that direction. To fix your issue, you'd probably need to have a variable to handle the speed seperately so you can use the movement variable for directional inputs. Sorry that my answer didn't help.