I have a class Character. In this class I use a Coroutine to move the gameobject in the game
public class Character : MonoBehaviour
{
protected IEnumerator TranslateCorout(Vector3 destination, float speed)
{
Vector3 start = t.position;
Vector3 end = destination;
float interpoler = 0f;
while (interpoler <= 1f)
{
interpoler += speed;
t.position = Vector3.Lerp(start, end, interpoler);
yield return new WaitForSeconds(WaitTime);
}
}
}
I have then another class, which inherits from Character, with a function that uses the parent coroutine.
public class Hero : Character {
public void Walk()
{
anim.SetTrigger(WALKANIMATION);
Vector3 destination = new Vector3(t.position.x + 1f, t.position.y, t.position.z);
StartCoroutine(TranslateCorout(destination, movementspeed));
}
}
Aaaaand, it doesn’t work. He triggers the animation, but it does not move.
The TranslateCorout was in the hero class, before, and everything was fine. Now, putting in order the code, I moved it to the parent class (to avoid repetition of the code in other subclasses which want to translate).
The funny thing is: I cannot modify at all the trasform.position value in the parent class.
I can read it, I can -apparently- set it, but if I print it again, after modifying it, it’s not changed. And thus, the movement issue.
Why a corotuine can’t change the transform.position field, and the subclasses inherits correctly?
What am I doing wrong?
Thanks for the answers ^^