Question by
JebusChris · Apr 14, 2021 at 03:51 PM ·
inputjump
Character not jumping after switching to new input system
I am new to programming, and I am trying to make my character jump in a 2D platformer. I have switched to the new input system and now it will not jump with the code I used with the old system. I tried doing a Debug.Log to see if it is reacting to my button pressing, which it is. However, nothing else happens. Does anyone know why?
The code is as follows:
[SerializeField] float runspeed = 5f;
[SerializeField] float jumpSpeed = 15f;
Inputs inputs;
Rigidbody2D myRigidBody;
Animator myAnimator;
[SerializeField] LayerMask groundLayer;
Vector2 direction;
bool facingRight = true;
public bool onGround = false;
public float groundLength = 0.6f;
private void Awake()
{
inputs = new Inputs();
myRigidBody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
inputs.Player.Move.performed += ctx => direction = ctx.ReadValue<Vector2>();
inputs.Player.Move.canceled += ctx => direction = Vector2.zero;
}
private void OnEnable()
{
inputs.Player.Enable();
}
private void OnDisable()
{
inputs.Player.Disable();
}
private void Update()
{
onGround = Physics2D.Raycast(transform.position, Vector2.down, groundLength, groundLayer);
if(inputs.Player.Jump.triggered)
{
Jump();
}
}
private void FixedUpdate()
{
Run();
}
private void Run()
{
Vector2 playerMovement = new Vector2(direction.x, 0) * Time.deltaTime;
myRigidBody.MovePosition((Vector2)transform.position + (playerMovement * runspeed));
myAnimator.SetFloat("horizontal", Mathf.Abs(direction.x));
if((direction.x > 0 && !facingRight) || (direction.x < 0 && facingRight))
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
transform.rotation = Quaternion.Euler(0, facingRight ? 0 : 180, 0);
}
void Jump()
{
if(onGround)
{
Vector2 jumpVelocity = new Vector2(0f, jumpSpeed);
myRigidBody.velocity += jumpVelocity;
Debug.Log("Test");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Jumping in Unity C# 2 Answers
Making a fly mechanic while using the same button for jump 0 Answers
Character Jump only works randomly and only with the "Y" Button 0 Answers
Jump does not work with diagonal movement. 1 Answer
Jump Input Problem 0 Answers