How to alter the script joystick for 2D?

Greetings to all! :slight_smile:
There is such a script

using System;
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class Controller3DExample : MonoBehaviour
{
    public const float ROTATE_SPEED = 15f;

    public float movementSpeed = 5f;

    public CNAbstractController MovementJoystick;

    private CharacterController _characterController;
    private Transform _mainCameraTransform;
    private Transform _transformCache;
    private Transform _playerTransform;

    void Start()
    {
        // You can also move the character with an event system
        // You MUST CHOOSE one method and use ONLY ONE a frame
        // If you wan't the event based control, uncomment this line
        // MovementJoystick.JoystickMovedEvent += MoveWithEvent;

        _characterController = GetComponent<CharacterController>();
        _mainCameraTransform = Camera.main.GetComponent<Transform>();
        _transformCache = GetComponent<Transform>();
        _playerTransform = _transformCache.FindChild("Cocoon");
    }

   
    // Update is called once per frame
    void Update()
    {
        var movement = new Vector3(
            MovementJoystick.GetAxis("Horizontal"),
            0f,
            MovementJoystick.GetAxis("Vertical"));

        CommonMovementMethod(movement);
    }

    private void MoveWithEvent(Vector3 inputMovement)
    {
        var movement = new Vector3(
            inputMovement.x,
            0f,
            inputMovement.y);

        CommonMovementMethod(movement);
    }

    private void CommonMovementMethod(Vector3 movement)
    {
        movement = _mainCameraTransform.TransformDirection(movement);
        movement.y = 0f;
        movement.Normalize();

        FaceDirection(movement);
        _characterController.Move(movement * movementSpeed * Time.deltaTime);
    }

    public void FaceDirection(Vector3 direction)
    {
        StopCoroutine("RotateCoroutine");
        StartCoroutine("RotateCoroutine", direction);
    }

    IEnumerator RotateCoroutine(Vector3 direction)
    {
        if (direction == Vector3.zero) yield break;

        Quaternion lookRotation = Quaternion.LookRotation(direction);
        do
        {
            _playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, lookRotation, Time.deltaTime * ROTATE_SPEED);
            yield return null;
        }
        while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);
    }

}

I want to convert a 2D game. Change everything Vector3 on Vector2 and removes one variable. It turns out this code

using System;
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class Controller3DExample : MonoBehaviour
{
    public const float ROTATE_SPEED = 15f;

    public float movementSpeed = 5f;

    public CNAbstractController MovementJoystick;

    private CharacterController _characterController;
    private Transform _mainCameraTransform;
    private Transform _transformCache;
    private Transform _playerTransform;

    void Start()
    {
        // You can also move the character with an event system
        // You MUST CHOOSE one method and use ONLY ONE a frame
        // If you wan't the event based control, uncomment this line
        // MovementJoystick.JoystickMovedEvent += MoveWithEvent;

        _characterController = GetComponent<CharacterController>();
        _mainCameraTransform = Camera.main.GetComponent<Transform>();
        _transformCache = GetComponent<Transform>();
        _playerTransform = _transformCache.FindChild("Cocoon");
    }

   
    // Update is called once per frame
    void Update()
    {
        var movement = new Vector2(
            MovementJoystick.GetAxis("Horizontal"),
            MovementJoystick.GetAxis("Vertical"));

        CommonMovementMethod(movement);
    }

    private void MoveWithEvent(Vector2 inputMovement)
    {
        var movement = new Vector2(
            inputMovement.x,
            inputMovement.y);

        CommonMovementMethod(movement);
    }

    private void CommonMovementMethod(Vector2 movement)
    {
        movement = _mainCameraTransform.TransformDirection(movement);
        movement.Normalize();

        FaceDirection(movement);
        _characterController.Move(movement * movementSpeed * Time.deltaTime);
    }

    public void FaceDirection(Vector2 direction)
    {
        StopCoroutine("RotateCoroutine");
        StartCoroutine("RotateCoroutine", direction);
    }

    IEnumerator RotateCoroutine(Vector2 direction)
    {
        if (direction == Vector2.zero) yield break;

        Quaternion lookRotation = Quaternion.LookRotation(direction);
        do
        {
            _playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, lookRotation, Time.deltaTime * ROTATE_SPEED);
            yield return null;
        }
        while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);
    }

}

As a result of an error in this line

while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);

The error is due to Unity not knowing whether it should use Vector2 or Vector3. So do something like this:

while ((direction - (Vector2)_playerTransform.forward).sqrMagnitude > 0.2f);

Haven’t tried if the code actually works, but it gets rid of the error message anyhow…

Now, the error in this line

_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, lookRotation, Time.deltaTime * ROTATE_SPEED);

Weird, I don’t get any errors on that line…

NullReferenceException: Object reference not set to an instance of an object
Control+c__Iterator1.MoveNext () (at Assets/My/Control.cs:74)
UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
Control:FaceDirection(Vector2) (at Assets/My/Control.cs:64)
Control:CommonMovementMethod(Vector2) (at Assets/My/Control.cs:57)
Control:Update() (at Assets/My/Control.cs:40)

Is your _playerTransform actually set to something? If you use:

Debug.Log( "T: " + _playerTransform );

Is it null or is it set to something appropriate?

Not null

Really, weird, when I run the code it works (as long as I add a child object called “Cocoon” to the object which the script is on). The object moves around and I get no errors what so ever.

This script was designed for 3D object. I’m trying to use it for 2D object. Previously, he worked with errors. Now constantly pauses the game. I do not know what happened.