- Home /
Character facing wrong direction when moving vertically
Hello, I'm new to this, and I was wondering if you could give me some help.
I'm making a game with RPG like movement, where the character moves up, down, right and left. However, when it moves up or down I just want the character to face left or right, I don't have an animation for up or down.
The problem is that my character just tends to face right every time when it ONLY walks vertically, but I want it to face the direction that it was facing previously.
Here is a gif with an example, notice that when the character is primarily facing right then is fine, but when it walks facing left it switches between left and right and i just want it to face left: https://giphy.com/gifs/YKnPa7LqbuOD6ZcsDu
I'm sorry if I'm not explaining myself very well, please tell me if you don't understand what I want. Again, I'm very new at this.
Just in case, I'll put the code I use for movement. I primarily used the code from the Youtube channel Brackeys in RPG movement :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 2f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
//Input
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1){
animator.SetFloat("lastHorizontal", Input.GetAxisRaw("Horizontal"));
}
}
//Movement
void FixedUpdate(){
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
Hello :)
I can’t solve it but ask some questions.
I don’t know how the animator works.
Is the animator told to face the character to the right if the horizontal value (when you set SetFloat Horizontal) is 0 or higher?
In that case it should only be set to face the character to the right if the value is higher than 0 (not 0 itself).
Maybe do that in code: If(movement.x < 0 || movement.x > 0) animator.SetFloat("Horizontal", movement.x);
So only when you have pressed right or left on keyboard will we change SetFloat Horizontal.
And animator will always change direction of character only depending on SetFloat (lastHorizontal).
Which I Assume somehow changes the characters direction in the animator.
Come to think of it if you add this code you only need SetFloat(horizontal) to know in the animator which way to face the character.
Because it will only change if we press right or left on the keyboard.
LastHorizontal won’t be needed. Horizontal will always tell which way the character was facing ”last” anyway.
Thank you for your anwser :D! Sorry for the late response, I didn't see the notification for it.
But yeah, I will try what you said.
Your answer
Follow this Question
Related Questions
Interrupt a MoveTowards when triggering 2 Answers
A way to "freeze" the position of the animation? 1 Answer
Unity Android movement 1 Answer