moving sprite in new class won't work

Hi.

I made a new class for write method in it. I wrote a movement method in this class and and called it in the main class, but pos.x and pos.y won’t update and my sprite don’t move. where is my problem?

My interface Repository:

public interface IRepository
{
    void MoveToDirection(float speed, int animLayer, Animator anim, string paramName, bool bState);
}

My implement Class (Moving Method):

public class CharacterRepository : IRepository
{
    private bool Running = false;
    private float xPos, yPos;

    public CharacterRepository(float x, float y)
    {
        xPos = x;
        yPos = y;
    }

    public void MoveToDirection(float speed, int animLayer, Animator anim, string paramName, bool bState)
    {  
        if (Input.GetKey(KeyCode.RightArrow))
        {
            xPos += speed * Time.deltaTime;      
            bState = true;
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            xPos -= speed * Time.deltaTime;
            bState = true;
        }
        else
        {
            bState = false;
        }
        anim.SetBool(paramName, bState);
   }
}

My Main Class :

public struct PlayerParameter
{
    public float x;
    public float y;
}

public class PlayerController : MonoBehaviour
{
    private PlayerParameter player;
    private Animator anim;
    private IRepository repository;
    private float speed = 3f;

    // Control Animator state
    private bool Running = false;

    // Start is called before the first frame update
    void Start()
    {
        player = new PlayerParameter();
        anim = GetComponent < Animator >();
        repository = new CharacterRepository(player.x, player.y);
    }

    // Update is called once per frame
    void Update()
    {
        player.x = transform.position.x;
        player.y = transform.position.y;

        //StartCoroutine();

        repository.MoveToDirection(speed, 0, anim, "Running", Running);

        transform.position = new Vector2(player.x, player.y);
    }
}

Based on the above, CharacterRepository is missing a close brace and will not compile. Are you sure you’re running the code you think you are?

Also, put in some Debug.Log() statements here and there, see if all the code places you think are being executed actually are being executed.

It’s my mistake during copy from visual studio in forum and it can compile.

change animation state is working in CharacterRepository after call MoveToDirection in main class, but moving don’t work.

You are not moving the player.

Third script you load player.x/y and then store it, but you don’t change it.

The CharacterRepository, which implements the IRepository interface, updates a private xPos variable but nobody can see or use that. Maybe you didn’t finish the IRepository interface and it is supposed to somehow communicate that number back out?

1 Like

Thanks @Kurt-Dekker for your guide.

I found the problem.
I made a stupid mistake. I tried to set player.x/y to xPos/yPos in implements class for update character position. while I should do it vice versa.
I used a property and a constructor for control values between main and implements class.

This is correct code:

Implement Class:

public class CharacterRepository : IRepository
{
    private float _xpos;
    private float _ypos;

    // Set initial value from player position
    public CharacterRepository(float x, float y)
    {
        _xpos = x;
        _ypos = y;
    }

    public float XPos
    {
        get { return _xpos; }
        set { _xpos = value; }
    }
    public float YPos
    {
        get { return _ypos; }
        set { _ypos = value; }
    }

    public void MoveToDirection(float speed, int animLayer, Animator anim,  string paramName, bool bState)
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            _xpos += speed * Time.deltaTime;
            bState = true;      
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            _xpos -= speed * Time.deltaTime;
            bState = true;
        }
        else
        {
            bState = false;
        }

        anim.SetBool(paramName, bState);
}

Main Class :

public struct PlayerParameter
{
    public float x;
    public float y;
}

public class PlayerController : MonoBehaviour
{
    private PlayerParameter player;
    private Animator animator;
    private CharacterRepository repository;
    private float speed = 3f;

    private bool Running = false;

    // Start is called before the first frame update
    void Start()
    {
        player = new PlayerParameter();
        animator = GetComponent < Animator >();
        player.x = transform.position.x;
        player.y = transform.position.y;
        repository = new CharacterRepository(player.x, player.y);
    }

    // Update is called once per frame
    void Update()
    {
        repository.MoveToDirection(speed, 0, animator, "Running", Running);

        player.x = repository.XPos;
        player.y = repository.YPos;

        transform.position = new Vector2(player.x, player.y);
    }
}
1 Like