Do parameters harm physics in Unity update method?

Asked

Viewed 46 times

-1

Well, basically I created a Playercontroller class and was separating some methods until creating a Playermechanics method(float jumpForce, etc...). When placing this method inside Update() I realized that the physics of jumping bugged and the character teleported in coordinate y with the value of jumpForce. the problem is that in the Playercontroller method I used Time.deltaTime to make the calculation of gravity, but when I take the parameters and call the variables directly all commands work perfectly!

I’ll put the code snippet that works and down what!

Bug-free code

void PlayerMechanics()
    {
        if (m_controller.isGrounded)
        {
            perceivedForceOnTheAxis = MovementInZAxis(Input.GetAxis("Vertical"), speedMovement);
            perceivedForceOnTheAxis.y = Jump(Input.GetButtonDown("Jump"), jumpForce);
        }
        m_controller.transform.Rotate(RotationInYAxis(Input.GetAxisRaw("Horizontal"), speedRotation) * Time.deltaTime);
>        perceivedForceOnTheAxis.y -= gravity * Time.deltaTime;
>        m_controller.Move(perceivedForceOnTheAxis * Time.deltaTime);
    }

void Update()
    {
>       PlayerMechanics();
    }

Bug code

void PlayerMechanics(CharacterController m_controller, float perceivedForceOnTheAxis, float gravity, float speedMovement, float jumpForce, float speedRotation)
    {
        if (m_controller.isGrounded)
        {
            perceivedForceOnTheAxis = MovementInZAxis(Input.GetAxis("Vertical"), speedMovement);
            perceivedForceOnTheAxis.y = Jump(Input.GetButtonDown("Jump"), jumpForce);
        }
        m_controller.transform.Rotate(RotationInYAxis(Input.GetAxisRaw("Horizontal"), speedRotation) * Time.deltaTime);
>        perceivedForceOnTheAxis.y -= gravity * Time.deltaTime;
>        m_controller.Move(perceivedForceOnTheAxis * Time.deltaTime);
    }

void Update()
    {
>       PlayerMechanics(m_controller, perceivedForceOnTheAxis, gravity, speedMovement, jumpForce, speedRotation);
    }

The problem is exclusively when I invent to put the parameters!!! I do not know if it is a bad practice, but I found it interesting to bugger just for this reason.

I had thought to do so for soon when I saw the parameters I would already know which variables I was using in that method!

2 answers

0

Look what the friend above said is true, but give you a hint of POO here, if you have the variables you need, during the method call in class, there is no reason to use parameters. But what is happening in your code, is not that parameters Ugam, but when you call something by parameter it does not change the original variable, so in your case you are always passing the same values to Playermechanics, you could solve this by putting "ref" in front of the parameters. To give you an example I’ll do a simulation:

   m_controller = this.GetComponent<CharacterController>();
   perceivedForceOnTheAxis = 1;
   gravity = 9.8f;
   speedMovement = 3;
   jumpForce = 3;
   speedRotation = 2;
    void PlayerMechanics()
        {
            if (m_controller.isGrounded)
            {
                perceivedForceOnTheAxis = MovementInZAxis(Input.GetAxis("Vertical"), speedMovement);
                perceivedForceOnTheAxis.y = Jump(Input.GetButtonDown("Jump"), jumpForce);
            }
            m_controller.transform.Rotate(RotationInYAxis(Input.GetAxisRaw("Horizontal"), speedRotation) * Time.deltaTime);
    >        perceivedForceOnTheAxis.y -= gravity * Time.deltaTime;
    >        m_controller.Move(perceivedForceOnTheAxis * Time.deltaTime);
        }

    void Update()
        {
    >       PlayerMechanics();
        }

In the end, the variables would change their value after calling the Playermechanics method, because you touched them directly.

m_controller = this.GetComponent<CharacterController>();
       perceivedForceOnTheAxis = 1;
       gravity = 9.8f;
       speedMovement = 3;
       jumpForce = 3;
       speedRotation = 2;

void PlayerMechanics(CharacterController m_controller, float perceivedForceOnTheAxis, float gravity, float speedMovement, float jumpForce, float speedRotation)
    {
        if (m_controller.isGrounded)
        {
            perceivedForceOnTheAxis = MovementInZAxis(Input.GetAxis("Vertical"), speedMovement);
            perceivedForceOnTheAxis.y = Jump(Input.GetButtonDown("Jump"), jumpForce);
        }
        m_controller.transform.Rotate(RotationInYAxis(Input.GetAxisRaw("Horizontal"), speedRotation) * Time.deltaTime);
>        perceivedForceOnTheAxis.y -= gravity * Time.deltaTime;
>        m_controller.Move(perceivedForceOnTheAxis * Time.deltaTime);
    }

void Update()
    {
>       PlayerMechanics(m_controller, perceivedForceOnTheAxis, gravity, speedMovement, jumpForce, speedRotation);
    }

At the end of the call of the Playermechanics method, they would continue with the same value, because when you pass the parameter, you create a new instance in memory. Then you would be calling in Update always the same method with the same values, I believe it should be in loop going in the same direction and starting from scratch.

0

Hello, there is a method called Fixedupdate(), it is basically an Update that does not run on each frame but on a synchronization rate based on the physics of Unity, perfect for interactions involving movement and physics.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.