-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!