翻转精灵与奇怪的运动脚本统一

Flipping sprite in unity with weird movement script

我想根据它们前进的方向翻转我的精灵,然后保持在那个位置直到它们再次移动。我已经尝试过速度检测并相应地翻转精灵,但由于我的移动不是靠力而是通过改变位置它无法检测速度(我认为),所以任何建议都会有所帮助。

这是我目前得到的移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{

    //DRAG AND DROP
    bool isBeingDragged = false;
    //POSITIONS AND TARGET
    public float speed = 0.5f;
    float timer = 0f;
    public float waitTime;
    public float distanceToAchieve = 0.2f;
    bool isFollowing = true;
    bool isWaiting = false;
    //GOAL TARGET
    public GameObject goalGameObject;
    Vector2 target;
    public Vector2 bounds = new Vector2(1.75f, 3);
    private Vector2 RP;
    private float startPosX;
    private float startPosY;

    void Start()
    {

        if (this.gameObject.tag == "1")
        {
            transform.position = RandomizePosition();
        }
        target = RandomizePosition();
        isFollowing = true;


    }
    private void Update()
    {

        if (!isBeingDragged)
        {
            if (!isWaiting && Vector2.Distance(transform.position, target) < distanceToAchieve)
            {
                isWaiting = true;
                isFollowing = false;
                RandomizeGoal();
            }
            if (isWaiting)
            {
                timer += Time.deltaTime;
                if (timer > waitTime)
                {
                    timer = 0f;
                    isWaiting = false;
                    isFollowing = true;
                }
            }
        }
    }

    private void FixedUpdate()
    {
        if (isFollowing && !isBeingDragged)
        {
            transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
        }
        if (isBeingDragged)
        {
            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
        }

    }
    Vector2 RandomizePosition()
    {
        RP = new Vector2(transform.position.x - Random.Range(-1f, 1f), transform.position.y - Random.Range(-1f, 1f));
        if (RP.x < bounds.x && RP.x > bounds.x * -1 && RP.y < bounds.y && RP.y > bounds.y * -1)
        {
            return RP;
        }
        else
        {
            return new Vector2(transform.position.x, transform.position.y);
        }
    }
    void RandomizeGoal()
    {
        waitTime = Random.Range(2, 10);
        target = RandomizePosition();
        goalGameObject.transform.position = target;
    }
    private void OnMouseDown()
    {
        isBeingDragged = true;

        Vector3 mousePos;
        mousePos = Input.mousePosition;
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);

        startPosX = mousePos.x - this.transform.localPosition.x;
        startPosY = mousePos.y - this.transform.localPosition.y;
    }
    private void OnMouseUp()
    {
        isBeingDragged = false;
        GetComponent<Merging>().CheckNearest();
    }
}
    private void FixedUpdate()
    {
        if (isFollowing && !isBeingDragged)
        {
            transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
        }
        if (isBeingDragged)
        {
            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            // capture the current position of target
            // by the way, why you use "loclPosition" for moving ?? 
            // I don't thinkg this is appropriate. 
            var currentPos = this.gameObject.transform.localPosition;

            var newPos = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);


            // then compare this with new position
            bool isLeftSided = false;
            if (newPos.x > currentPos.x)
            {
                isLeftSided = true;
            }
            else
            {
                isLeftSided = false;
            }
            // And figure out the direction . in this example, I wrote just right-left case
            // use the y pos for up and down  
            // then flip the sprite ( this case, default is right sided )
            this.gameObject.GetComponent<SpriteRenderer>().flipX. = isLeftSided;

            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
        }

    }