My issue starts with the distance itself. I’ve tried multiple ways around it and even using rigidbody velocity to check (but for some reason, no matter what it always stays at 0) but still nothing. It might be the Photon that’s the issue? I’m not sure. Anyway here’s the problem:
isMoving is false if the player is standing still, and isMoving is supposed to be true whenever the player starts moving, and that will set the bool to true. But instead, it runs through every frame, and constantly sets it to false and true really quickly. So when i’m standing still, the proper animation plays, but when im moving, it restarts it every frame because it thinks its false? I’m not the best at explaining…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerMovement : MonoBehaviour
{
PhotonView view;
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Animator anim;
public Camera cam;
bool isMoving;
Vector2 movement;
Vector2 mousePos;
Vector2 prevPos;
float dist;
private void Start()
{
view = GetComponent<PhotonView>();
rb = GetComponent<Rigidbody2D>();
anim = GetComponentInChildren<Animator>();
}
void Update()
{
if(!view.IsMine) { return; }
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
dist = Vector2.Distance(prevPos, transform.position);
if (dist < 0.001f)
{
isMoving = false;
}
else if (dist >= 0.001f)
{
isMoving = true;
}
prevPos = transform.position;
LookAtMouse();
Debug.Log(isMoving);
}
private void FixedUpdate()
{
if (!view.IsMine) { return; }
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
void LookAtMouse()
{
Vector2 mousePos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.up = mousePos - new Vector2(transform.position.x, transform.position.y);
}
}