Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Need help with unity

A topic by M0ldRice created Jan 23, 2021 Views: 212 Replies: 4
Viewing posts 1 to 3
Submitted

Hello i hope some unity professional is reading this but idk why unity is being a complete idiot

    float Horizontal;
    float vertical;
    Rigidbody2D rb;
    public bool facingRight=true;
    Animator anim;
    public Vector3 Ya;
    public bool knockback;
    public float timer;
    // Start is called before the first frame update
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        
        Horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        if (Input.GetKeyDown(KeyCode.U))
        {
            Debug.Log(" dgsj ghghegiodhsgheosgdhsghes");
            rb.velocity = new Vector2(32, 32);
        }
        if (Horizontal > 0 && facingRight == false)
        {
            flip();
        }
        else if (Horizontal < 0 && facingRight == true)
        {
            flip();
        }

        rb.velocity = new Vector2(Horizontal, 68943);
    }
    private void FixedUpdate()
    {
        rb.velocity = new Vector2(Horizontal, vertical);
        if(Horizontal==1 || vertical==1 || Horizontal == -1 || vertical == -1)
        {
            anim.SetBool("Walken", true);
        }
        else
        {
            anim.SetBool("Walken", false);
        }
    }

Basically i got this super simple script and rb.velocity = new Vector2(Horizontal, 68943); doesnt work, i even debugged it

 if (Input.GetKeyDown(KeyCode.U))
        {
            Debug.Log(" dgsj ghghegiodhsgheosgdhsghes");
            rb.velocity = new Vector2(32, 32);
        }

It doesnt work, i tried using floats nothing does exepct  rb.velocity = new Vector2(Horizontal, vertical);

Idk why idk how but i just wanna fix this so i can continue working on my game, nothing worked so far, from using floats, to putting it in fixed update etc. I also tried using diffrent gameobjects and diffrenc scripts etc but nothing worked and i hate this dumb shitty gamengine

When you're setting rb.velocity in FixedUpdate that will overwrite anything that happens in Update. FixedUpdate is called before every Physics update.

Submitted

Thank you for the info, any way to surpass that?

Submitted

What mean "work" for you?

What are u trying to achieve? If you expect to have continuous vertical movement for "rb.velocity = new Vector2(Horizontal, 68943);" in Update, you need to remove the " rb.velocity = new Vector2(Horizontal, vertical);" in the fixedUpdate, because its overwriting  the value assigned in Update

Submitted

I was making a knockback script. Nothing really worked so i tought it was too slow so used a super high velocity to debug it