1
\$\begingroup\$

I'm making a 2D platformer game with Unity and I have some problems with the new Input System. Also, I'm using PlayerInput component with invoke unity events behavior. In fact, I downloaded a player controller code from GitHub to use it as a guide so I used it in my actual player controller. But there is a problem and that is the code is using the old Input and I don't know how to change "Input.GetAxis" and what is the equivalent in the new system. I've read many documents about it and I know that there is no official equivalent but there should be a way to get the same output. And other than this you may find some mistakes in the code and I would appreciate If you guide me. Thanks in advance.

This is my Player Controller.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    private PlayerInputActions controls;

    private Rigidbody2D rb;
    private Animator anim;
    private bool facingRight = true;
    private float moveInput;
    public Transform feetPos;
    public float jumpInput;
    public float speed;

    [SerializeField] float JumpVelocity = 5;
    float JumpPressedRemember = 0;
    [SerializeField] float JumpPressedRememberTime = 0.2f;

    float GroundedRemember = 0;

    [SerializeField] float GroundedRememberTime = 0.25f;
    [SerializeField] float HorizontalAcceleration = 1;
    [SerializeField] [Range(0, 1)] float HorizontalDampingBasic = 0.5f;
    [SerializeField] [Range(0, 1)] float HorizontalDampingWhenStopping = 0.5f;
    [SerializeField] [Range(0, 1)] float HorizontalDampingWhenTurning = 0.5f;
    [SerializeField] [Range(0, 1)] float JumpHeight = 0.5f;

    private void Awake()
    {
        controls = new PlayerInputActions();
    }

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        moveInput = context.ReadValue<float>();
    }

    public void OnJump(InputAction.CallbackContext context)
    {
        JumpVelocity = context.ReadValue<float>();
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
      
        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInput < 0)
        {

            Flip();
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }

    void Update()
    {
        Vector2 GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
        Vector2 GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
        bool Grounded = Physics2D.OverlapBox(GroundedBoxCheckPosition, transform.localScale, 0);

        GroundedRemember -= Time.deltaTime;

        if (Grounded)
        {
            GroundedRemember = GroundedRememberTime;
        }

        JumpPressedRemember -= Time.deltaTime;

        if (controls.Player.Jump.triggered)
        {
            JumpPressedRemember = JumpPressedRememberTime;
        }

        if (controls.Player.Jump.triggered)
        {
            if (rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * JumpHeight);
            }
        }

        if ((JumpPressedRemember > 0) && (GroundedRemember > 0))
        {
            JumpPressedRemember = 0;
            GroundedRemember = 0;
            rb.velocity = new Vector2(rb.velocity.x, JumpVelocity);
        }

        float HorizontalVelocity = rb.velocity.x;
        HorizontalVelocity += Input.GetAxisRaw("Horizontal");

        if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
            HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenStopping, Time.deltaTime * 10f);
        else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(HorizontalVelocity))
            HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenTurning, Time.deltaTime * 10f);
        else
            HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingBasic, Time.deltaTime * 10f);

        rb.velocity = new Vector2(HorizontalVelocity, rb.velocity.y);
    }
}

And this is the code that I downloaded from GitHub.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
[SerializeField]
LayerMask lmWalls;

[SerializeField]
float fJumpVelocity = 5;

Rigidbody2D rigid;

float fJumpPressedRemember = 0;
[SerializeField]
float fJumpPressedRememberTime = 0.2f;

float fGroundedRemember = 0;
[SerializeField]
float fGroundedRememberTime = 0.25f;

[SerializeField]
float fHorizontalAcceleration = 1;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingBasic = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenStopping = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenTurning = 0.5f;

[SerializeField]
[Range(0, 1)]
float fCutJumpHeight = 0.5f;

void Start ()
{
    rigid = GetComponent<Rigidbody2D>();
}

void Update ()
{
    Vector2 v2GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
    Vector2 v2GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
    bool bGrounded = Physics2D.OverlapBox(v2GroundedBoxCheckPosition, v2GroundedBoxCheckScale, 0, lmWalls);

    fGroundedRemember -= Time.deltaTime;
    if (bGrounded)
    {
        fGroundedRemember = fGroundedRememberTime;
    }

    fJumpPressedRemember -= Time.deltaTime;
    if (Input.GetButtonDown("Jump"))
    {
        fJumpPressedRemember = fJumpPressedRememberTime;
    }

    if (Input.GetButtonUp("Jump"))
    {
        if (rigid.velocity.y > 0)
        {
            rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * fCutJumpHeight);
        }
    }

    if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0))
    {
        fJumpPressedRemember = 0;
        fGroundedRemember = 0;
        rigid.velocity = new Vector2(rigid.velocity.x, fJumpVelocity);
    }

    float fHorizontalVelocity = rigid.velocity.x;
    fHorizontalVelocity += Input.GetAxisRaw("Horizontal");

    if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
        fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenStopping, Time.deltaTime * 10f);
    else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(fHorizontalVelocity))
        fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenTurning, Time.deltaTime * 10f);
    else
        fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingBasic, Time.deltaTime * 10f);

    rigid.velocity = new Vector2(fHorizontalVelocity, rigid.velocity.y);
}

  
\$\endgroup\$
5
  • 1
    \$\begingroup\$ In the old system, an input axis could be both an analog input (like a gampead thumbstick) or a digital input (like cursor keys on a PC keyboard). In the new Input system, those are handled separately. Which one of the two do you want to use here? \$\endgroup\$
    – Philipp
    Sep 24, 2020 at 16:28
  • \$\begingroup\$ Thanks for your reply. I want to use the new Input System here. I already defined the controls for gamepad and keyboard with Input Actions window in Unity. \$\endgroup\$
    – Ali
    Sep 24, 2020 at 17:03
  • \$\begingroup\$ @Philipp In fact, there are some lines of code in here that are for the old system and I want to change them to something compatible with the new Input System and get the same output as the old input. \$\endgroup\$
    – Ali
    Sep 24, 2020 at 23:01
  • 2
    \$\begingroup\$ @Ali That's not what Philipp was asking you. You already said you want to use the new input system in your question, so you don't have to repeat yourself in the comments. What Philipp is saying is that the intent of the code you've shown is ambiguous. There are two different ways the Input Manager could have been configured in the old system to work with this code, and each way would behave differently. To get the same behaviour from the new input system, we need to understand which of these styles you want to use - analog or digital. We need this info to help you translate this to the new sys \$\endgroup\$
    – DMGregory
    Sep 25, 2020 at 1:20
  • \$\begingroup\$ @DMGregory Okay, I didn't understand his question. I want to have both gamepad and keyboard working. But I think I need to use an analog input. \$\endgroup\$
    – Ali
    Sep 25, 2020 at 1:46

1 Answer 1

0
\$\begingroup\$

Finally, I can now move my character. Jumping still has some problems which I will cover it on my next questions. I asked this question on Stack Overflow and I was able to fix my problem with the help of a friend. Since the answer is long and it continues with more than 10 comments, I decided to put the link to my question here.

https://stackoverflow.com/questions/64019057/i-have-some-serious-problems-with-new-input-system/64066014?noredirect=1#comment113359107_64066014

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .