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);
}
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);
}
}
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?
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);
}
}