- Home /
How to make the player controller movement and jumping and physics into the new unity input system
Hi, I was watching a couple of the new unity input system tutorials so the game can be playable in local multiplayer and I was trying to implement my player controller into the unity input system but some bugs were happening like some of the rigid body values was not correct, in other words, I have player controller script and I want to implement it in unity's input system by using this function private void Move(InputAction.CallbackContext context)
, I hope that I explained my issue well
player controller script:
void FixedUpdate()
{
moveCharacter(direction.x);
if (jumpTimer > Time.time && onGround)
{
animator.SetTrigger("takeOf");
Jump();
}
else
{
animator.SetBool("isJumping", true);
}
modifyPhysics();
}
void moveCharacter(float horizontal)
{
rb.AddForce(Vector2.right * horizontal * moveSpeed);
if ((horizontal > 0 && !facingRight) || (horizontal < 0 && facingRight))
{
Flip();
}
if (horizontal == 0)
{
animator.SetBool("isRunning", false);
}
else
{
animator.SetBool("isRunning", true);
}
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
jumpTimer = 0;
}
void modifyPhysics()
{
bool changingDirections = (direction.x > 0 && rb.velocity.x < 0) || (direction.x < 0 && rb.velocity.x > 0);
if (onGround)
{
animator.SetBool("isJumping", false);
if (Mathf.Abs(direction.x) < 0.4f || changingDirections)
{
rb.drag = linearDrag;
}
}
if (!onGround)
{
rb.gravityScale = gravity;
rb.drag = linearDrag * 0.15f;
if (rb.velocity.y < 0)
{
rb.gravityScale = gravity * fallMultiplier;
}
}
}
void Flip()
{
facingRight = !facingRight;
transform.rotation = Quaternion.Euler(0, facingRight ? 0 : 180, 0);
}
unity's input system functions:
private void Move(InputAction.CallbackContext context)
{
}
private void Jump(InputAction.CallbackContext context)
{
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Only one player for the host but two for the client, 0 Answers
Master Server GUI not showing up [Errors finally coming up in console] 1 Answer
Store Data Online 0 Answers