2022-10-27

Weird rb.velocity.y

When I'm moving left or right rb.velocity.y changes to weird numbers. How can I get rid of this? It really annoing because I want to change the gravity when player is jumping and because of this bug gravity changes even if player is moving left and right.

My code:

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

public class movePlayer : MonoBehaviour
{
    public ParticleSystem dust;
    public Animator animator;
    [SerializeField] public float speed;
    private float moveInput;
    public float jump;  
    private Rigidbody2D rb;
    public BoxCollider2D coll;
    private bool facingRight = true;

    [SerializeField]private bool isGrounded;
    public Transform GroundCheck;
    public LayerMask WhatIsGround;
    public float fallMultiplier = 3.5f;
    public float lowJumpMultiplier = 3f;
    private bool jumped = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    void Update()
    {
        //isGrounded = Physics2D.OverlapCircle(GroundCheck.position,checkRadius,WhatIsGround);
        // if (Input.GetKey(KeyCode.RightArrow)) moveInput = 1;
        // else if (Input.GetKey(KeyCode.LeftArrow)) moveInput = -1;
        // else moveInput = 0;
        moveInput = Input.GetAxis("Horizontal");
        if (!Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)) {
            moveInput = 0;
        }
        Debug.Log(moveInput);
        animator.SetFloat("speed", MathF.Abs(moveInput));

        if(facingRight == false && moveInput > 0){
            Flip();
            CreateDust();
        }
        else if(facingRight == true && moveInput < 0){
            Flip();
            CreateDust();
        }

        if(isGrounded == false)
        {
            animator.SetBool("isJumping", true);
        }

        if(isGrounded == true)
        {
 
            animator.SetBool("isJumping", false);
        }
        
        if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow))  && isGrounded == true)
        {
            CreateDust();
            jumped = true;
            
        }  

    }

    void FixedUpdate(){

        isGrounded = Physics2D.BoxCast(coll.bounds.center,coll.bounds.size, 0, Vector2.down, .1f,WhatIsGround);

        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

        if(jumped){
            rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
            jumped = false;
        }

        if(rb.velocity.y < 0){
            rb.gravityScale = fallMultiplier;
        }
        else if(rb.velocity.y > 0 && !(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow))){
            rb.gravityScale = lowJumpMultiplier;
        }
        else{
            rb.gravityScale = 1f;
        }  
    }

    void Flip(){
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }

    void CreateDust(){
        dust.Play();
    }
}

I have tried changing how I mover the player but it didn't change anything.



No comments:

Post a Comment