Что делать если из-за маленького FPS не успевает выполняться очистка вектора?

Рейтинг: 0Ответов: 0Опубликовано: 09.04.2023

При резком повороте мой персонаж ненадолго подлетает из-за вектора При каждом повороте на земле и не в прыжке я очищаю вектор, но при маленьком FPS он очистка происходит уже не на земле и вектор не обновляется.

{
    //Walk
    public float Speed = 7;
    public Vector2 movement;
    //Flip
    public bool Right = true;

    //Jump, Dash
    public bool onGround;

    //Jump
    public float JumpForce = 60;
    public float JumpControlTime = 0.3f;

    private bool JumpControl;
    private float JumpTime = 0f;
    //SecondJump
    public float DoubleJumpForce = 15;
    public int JumpCount = 2;

    private int MomentJumpCount = 0;

    //Dash
    public int DashImpulse = 5000;

    //BugFix
    public bool CanSlide = false;
    public PhysicsMaterial2D NoSlide;
    public PhysicsMaterial2D Slide;

    public bool CleanVectorY = false;

    //functions
    private Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        //Application.targetFrameRate = 1000;
    }

    void FixedUpdate()
    {
        Jump();
        BugFix();
    }
    void Update()
    {
        Walk();
        SecondJump();
        Dash();
        BugFixFPS();
    }

    void BugFixFPS()
    {
        if ((onGround) && !JumpControl)
        {
            CleanVectorY = true;
        }
        else
        {
            CleanVectorY = false;
        }
    }

    void BugFix()
    {
        if (movement.x == 0 && !CanSlide)
        {
            rb.sharedMaterial = NoSlide;

        }
        else
        {
            rb.sharedMaterial = Slide;

        }

        if ((onGround) && !JumpControl)
        {
            CleanVectorY = true;
        }
        else
        {
            CleanVectorY = false;
        }
    }


    void Walk()
    {
        movement.x = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(movement.x * Speed, rb.velocity.y);

        if (movement.x < 0 && Right == true)
        {
            Flip();
            Debug.Log("в лево");
        }
        else if (movement.x > 0 && Right == false)
        {
            Flip();
            Debug.Log("в право");
        }
    }
    public void Flip()
    {
        if (CleanVectorY)
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
            Debug.Log("Очистить вектор");
        }

        Vector3 scale = transform.localScale;
        scale.x *= -1;
        transform.localScale = scale;
        Right = !Right;
    }

    void Jump()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            if (onGround)
            {
                if (!JumpControl)
                {
                    rb.velocity = new Vector2(rb.velocity.x, 0);
                    Debug.Log("Обновление вектора => Прыжок");
                }
                JumpControl = true;

            }
        }
        else
        {
            JumpControl = false;
        }
        if (JumpControl)
        {
            if ((JumpTime += Time.fixedDeltaTime) < JumpControlTime)
            {
                rb.AddForce(Vector2.up * JumpForce / (JumpTime * 10));
            }
        }
        else
        {
            JumpTime = 0;
        }

        if (onGround)
        {
            MomentJumpCount = 0;
        }
    }
    void SecondJump()
    {
 
        if (Input.GetKeyDown(KeyCode.Space) && !onGround && !JumpControl && (++MomentJumpCount < JumpCount))
        {
            rb.velocity = new Vector2(0, DoubleJumpForce);
            Debug.Log(MomentJumpCount + "-Доп.прыжок");
        }
    }

    void Dash()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
            if (!Right)
            {
                rb.AddForce(Vector2.left * DashImpulse);
            }
            else
            {
                rb.AddForce(Vector2.right * DashImpulse);
            }
            Debug.Log("Рывок!");
        }
    }
}

Ответы

Ответов пока нет.