Tile Vania Movement isn't working (Unity 2022)

I put this in the code like Rick said:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour

{

    Vector3 moveInput;

    Rigidbody2D myRigidbody;

    void Start()

    {

        myRigidbody = GetComponent<Rigidbody2D>();

    }

    void Update()

    {

        Run();

    }

    void onMove(InputValue value)

    {

        moveInput = value.Get<Vector3>();

        Debug.Log(moveInput);

    }

   

    void Run()

    {

        myRigidbody.velocity = moveInput;

    }

}

I also put the input system too.

Can anyone help me?

There is a few Problems with the code:
First you move input should not be a Vector3 it should only be Vector2

Vector2 moveInput;

Moving on there is a few problems here,


 void onMove(InputValue value)

    {

        moveInput = value.Get<Vector3>();

        Debug.Log(moveInput);

    }

This is case Sensitive:

  void onMove(InputValue value)

Should be,

   void OnMove(InputValue value)

your move input should not be getting a vector 3 value, if you rembered you declered at the top that moveInput is now a Vector2

        moveInput = value.Get<Vector3>();

Which means it should be set as such:

 moveInput = value.Get<Vector2>();

Setting your ridgidbody velocity to your move input is not going to work.
It should look more like this.

     //create local variable and assign a new vector2 based on the player input for the x * runspeed and set the y to the ridgidbody y

        Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;

Hope this helps explain it a bit better.

It still dosn’t work, though.

Ok well at this point, I can not see what is going wrong. Please can you do the following

  1. Post your updated code.
  2. Post a Screenshot of your player input manager
    3)A screenshot of the inspector panel with your player selected.

When asking for help it is beneficial you provide the most information as possible.There is more than code when working on games, including what scripts is attached, how the the input system and such is set up.

Hi moks,

In addition to what @LaniganDev wrote, it would also be great if you could write what you tried to no avail because, otherwise, we don’t know if you even tried what we suggested. (In this case: what LaniganDev suggested.)

@LaniganDev this is the updated code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour

{

    Vector2 moveInput;

    Rigidbody2D myRigidbody;

    [SerializeField] float runSpeed = 5f;

    void Start()

    {

        myRigidbody = GetComponent<Rigidbody2D>();

    }

    void Update()

    {

        Run();

    }

    void OnMove(InputValue value)

    {

        moveInput = moveInput;

        moveInput = value.Get<Vector2>();

        Debug.Log(moveInput);

    }

   

    void Run()

    {

        Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, myRigidbody.velocity.y);

        myRigidbody.velocity = playerVelocity;

    }

}

What value does moveInput have while your game is running? What value(s) do you get if you press the associated keys on your keyboard? And what values do you get if no key is pressed?

Are there any error messages in your console while your game is running?

There are no errors.

MoveInput stores the value of the key that is pressed.

However, I get (0.00, 0.01) in the console if the up key (and up key only) is pressed.

And even though it shows it in the conslole, it dosn’t actually move the player.

That might be the problem. Actually, the input value should be |1|. Check your input actions. Are they set up exactly as Rick’s?

If you cannot spot any difference, you could write a little workaround by setting the input values that are higher than 0 to 1:

void OnMove(InputValue value)
{
    moveInput = value.Get<Vector2>();

    moveInput.x = (moveInput.x == 0f) ? 0f : Mathf.Sign(moveInput.x); 
    moveInput.y = (moveInput.y == 0f) ? 0f : Mathf.Sign(moveInput.y); 
   
    Debug.Log(moveInput);
}

Given there is nothing wrong with the input system, this should get you the expected values thanks to the ternary operator and Mathf.Sign.

If your player moves as expected with this solution, the problem was caused by the input system giving you the “wrong” values.

Sorry, I meant to say that the console prints (0.00, -1.00)

And the first argument remains 0 even if you press the left or the right arrow?

In the Run method, we calculate moveInput.x * runSpeed. If runSpeed is 0 or close to 0, the player looks as if he doesn’t move at all.

Yes

I set the runSpeed to 25 and when I press up, the player dosn’t move.

and when I set the runSpeed to 1000 the player still dosn’t move.

That’s really odd. Where did you set runSpeed to 25? In the Inspector? Since we exposed that variable with [SerializeField], the Inspector controls the value. If you change the value at the top of your code, the Inspector ignores the changes because only the value in the Inspector is valid for this component.

Also double-check if the PlayerMovement script is attached to your player in the Hierarchy. Maybe it is attached to the wrong game object, e.g. an invisible game object. In that case, your PlayerMovement object works perfectly fine but the outcome in the game is still unexpected because you obviously expect the player to move, not some other game object.

You could log the name of the game object to which this component is attached into your console, for example, like this:

Debug.Log("PlayerMovement attached to: " + name, gameObject);

When the message appears in your console, click it once to see which game object gets highlighted in your Hierarchy. That’s the purpose of the second argument (gameObject) of the Debug.Log method call.

The error messages are enabled in your Console, aren’t they? Check the three buttons in the top right corner of the Console. Maybe there is a NullReferenceException because the player’s Rigidbody2D component was not found. That would explain why the user input works as expected but the player does not move.

Yes I did.

The script is on the the player and the Prefab.

And to triple checked, and I did what you said and I got this:

Screenshot 2024-07-23 215722

There is no errors, I also clicked Pause On Error and no errors.

Could you please fill out our form and send me your zipped project (without the Temp and Library folders)? Don’t forget to add a link to this thread.

When you are done, drop me a line here, so I can check our server.

Privacy & Terms