PlayerController is not responding anymore

Hey guys,

I’m learning Unity (First game) and I am stuck.

I want to create a simple 2D topdown game zelda like.

I created a simple player controller through a script.

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

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    private bool isMoving;
    private Vector2 input;


void OnGUI() 
    {
    Event e = Event.current;
    if (e.isKey)
      Debug.Log("e.keyCode: "+e.keyCode);
    }

    private void Update()
    {
    
        if (isMoving)
        {
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Verical");

            if (input != Vector2.zero)
            {
                var targetPos = transform.position;
                targetPos.x += input.x;
                targetPos.y += input.y;

                StartCoroutine(Move(targetPos));
                Debug.Log("it is working");

            }
        }
    }

    IEnumerator Move(Vector3 targetPos)
    {
        isMoving = true;
        while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
            yield return null;
        }
        transform.position = targetPos;

        isMoving = false;

    }


}

It was working perfectly.

Then I tried to implement the new input system to learn how to use it.
Switched the player parameters “Active Input Handling”.

I was also perfectly working.

Then I switch back to the old input system.

Now I can’t move my player anymore.
On this project nor any new ones I create.

I don’t know what cause it. Is it the script, a corrupt package, the input system, the controller system, etc?

I also tried the following:

  • Create a new project and strat again from scratch
  • Restart Unity
  • Restart the computer
  • Delete all the cache
  • Delete every single Unity files from the computer
  • Unistall/Reinstall Unity
  • Install a newer version (Unity 2022.3.40f1)
  • Tried Active Input Handling with old system
  • Tried Active Input Handling with new system
  • Tried Active Input Handling with both system

I just don’t know what to try next to solve this problem.
Maybe it is just something to tick I don’t know about.

Some extra insight for the code:
I can see the log of the key press in the console but don’t have the “it is working” log.

Do you have any insight or recommandation?

Thanks for helping guys :slight_smile:

Your player can only move if isMoving is true and I don’t see anywhere in your code where isMoving is set to true.

You’re moving your player by setting transform.position and so it’s movement won’t be blocked by obstacles. It’s best to use a rigidbody if you want movement to be blocked.

Also, your movement code is unnecessarily complicated. Try something like this:

using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
Rigidbody2D rb;
void Start()
{
    rb=GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"),0);
    rb.MovePosition(transform.position + input * moveSpeed * Time.deltaTime);
}
}

Thanks for your answer!
It is working on a new project but not the old one. :confused:

I will user your code on the new project. Thanks :slight_smile:

Just for me to understand, what do I need to modify in my script to make it work?
Regarding the isMoving, I don’t understand what is not working.

Change line 8 to:

private bool isMoving=true;
1 Like