Player doesn't face in direction of its last movement direction
This is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
public bool canMove;
void Start() {
canMove=true;
}
void Update() {
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (canMove) {
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
}
void FixedUpdate() {
if (canMove) {
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
}
I found a code that should fix this. The player is now facing in the right direction but it doesn't stop moving (so it's walking in one place). Where it changed the horizontal, vertical, and speed should be replaced by this:
if (movement != Vector2.zero)
{
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
Comment