Problems with fixed update and update

So i just had a problem with my jumping that i figured out because i had code with my input and physicis change in the same update when i needed to have my input on update and my rigid body changes in fixed update but now i have a problem where my movement is choppy because the same mistake with my jumping. so i just need to call my inputs in update and then make the changes to rigid body in fixed update, easy right? wrong im still a noob and dont understand how to do this through code. here is my code so far and my thought process.
My problematic code is this

void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontalInput, 0, verticalInput);
        Vector3 velocity = movement * playerspd;

        rb.AddForce(velocity * Time.fixedDeltaTime);
}

so i need to have my Verticle and Horizontal input in my update and then check if im pressing down an input button and move my rigid body through fixed update just like with jumping.
Here is what ive done so far
iv made a public bool called [public bool isMoving = false;
i then go into my update and type

if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
        {

            isMoving = true;
        } else
        {
            isMoving = false;
        }

Then i go into my fixed update and check if is moving is true [if(ismoving == true]
and put the rest of my code in but the problem is that my Vector3 variables Need my Input variable but they arent in the same brackets so they dont recognize eachother. for example if i type

if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
        {
            float horizontalInput = Input.GetAxis("Horizontal");
            float verticalInput = Input.GetAxis("Vertical");
            isMoving = true;
        } else
        {
            isMoving = false;
        }

and then do

if(isMoving == true)
        {
        Vector3 movement = new Vector3(horizontalInput, 0, verticalInput);
        Vector3 velocity = movement * playerspd;

        rb.AddForce(velocity * Time.fixedDeltaTime);
        }

that wont work because my Movement doesnt know what Horizontalinput or verticleinput is and i cant call them in the fixed update because its the same thing from the beginning. So im basically trying to do the impossible and call a variable from another function in the same script but i have no idea how im supposed to get around this and make my input communicate with eachother without completely deconstructing my varibles. I just finished watching a video on this an he basically explains what my problem is to a T


he mentions doing what im doing by having my input in update and them my physics in fixedupate and gives the other option of interpolation and a bunch of other things i have no idea about thaats overwhelming. I honestly didnt think it would be thi hard to make a ball move.
Sorry if this seems like a very inflated question for such a small problem but i just wanted to be as detailed and informing as possible because these Fixedupdate / update has consumed alot of my time from inexperience.

note to self. dont take brackeys vidoes as the word of god. The lerp on my camera is what cause the jagging looking problem. i ended up fixing it then scrapping the code for cinemachine

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms