Search code examples
c#unity-game-engine2d

movement works fine right but not left


I have a movement script that when I move right it is fine but when I move right it stutters. I have tried it without the camera moving same problem. I just cant understand why . The code is the same for both directions.

This is the code it gets it variables from other scripts but they don't have any other code than variables.

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

public class InputManager : MonoBehaviour
{
    Rigidbody2D rb;
    float jumpForce;
    float downForce;
    float speed;
    float maxSpeed;
    float maxJumps;
    float currentJumps;
    float cutJumpHeight;
    float jumpBufferTimer;
    float jumpBuffer;
    float jumpRememberTimer;
    float jumpRemember;
    float x;
    float y;
    bool isGrounded = false;
    AudioSource jumpSound;
    Jump jumpScript;
    Movement movementScript;
    GameObject[] groundCheckPoints;


    void Start()
    {
        jumpScript = GetComponent<Jump>();
        movementScript = GetComponent<Movement>();
        rb = GetComponent<Rigidbody2D>();
        groundCheckPoints = GameObject.FindGameObjectsWithTag("Check Point");
    }

    void Update()
    {
        MyInput();
        IsGrounded();
        FlipCharacter();
        jumpForce = jumpScript.jumpForce;
        speed = movementScript.speed;
        maxJumps = jumpScript.maxJumps;
        cutJumpHeight = jumpScript.cutJumpHeight;
        jumpRemember = jumpScript.jumpRememberTime;
        jumpBuffer = jumpScript.jumpBufferTime;
        jumpSound = jumpScript.jumpSound;
    }

    void FlipCharacter()
    {
        if (x > 0)
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }else if(x < 0)
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);
        }
    }

    void IsGrounded()
    {
        for (int i = 0; i < groundCheckPoints.Length; i++)
        {
            isGrounded = Physics2D.OverlapCircle(groundCheckPoints[i].transform.position, 0.1f, LayerMask.GetMask("Ground"));
            if (isGrounded)
            {
                break;
            }
        }
        Jump();
    }

    void FixedUpdate()
    {
            MovementPlatformer();
    }

    void MyInput()
    {
            x = Input.GetAxisRaw("Horizontal");
            y = Input.GetAxisRaw("Vertical");
    }

    void Jump()
    {
        jumpBufferTimer -= Time.deltaTime;
        jumpRememberTimer -= Time.deltaTime;

        if (isGrounded)
        {
            currentJumps = maxJumps;
        }

        if (currentJumps > 0)
        {
            jumpBufferTimer = jumpBuffer;
        }
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpRememberTimer = jumpRemember;
        }

        if (jumpBufferTimer > 0 && jumpRememberTimer > 0)
        {
            jumpBufferTimer = 0;
            jumpRememberTimer = 0;
            jumpSound.Play();

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            currentJumps--;
        }


        if (Input.GetKeyUp(KeyCode.Space) && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * cutJumpHeight);
        }
    }

    void MovementPlatformer()
    {
        rb.velocity = new Vector2(speed * x, rb.velocity.y);
    }

}

Solution

  • It was something to do with the flip character function when i moved it to fixed update it fixed it.