Search Unity

[Solved] Inheritance + Corotuines + transform.position

Discussion in 'Scripting' started by Micio_del_Cheshire, Apr 7, 2017.

  1. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    I have a class Character. In this class I use a Coroutine to move the gameobject in the game

    Code (CSharp):
    1. public class Character : MonoBehaviour
    2. {
    3.     protected IEnumerator TranslateCorout(Vector3 destination, float speed)
    4.     {
    5.         Vector3 start = t.position;
    6.         Vector3 end = destination;
    7.         float interpoler = 0f;
    8.  
    9.         while (interpoler <= 1f)
    10.         {
    11.             interpoler += speed;
    12.             t.position = Vector3.Lerp(start, end, interpoler);
    13.  
    14.             yield return new WaitForSeconds(WaitTime);
    15.         }
    16.  
    17.     }
    18. }
    I have then another class, which inherits from Character, with a function that uses the parent coroutine.

    Code (CSharp):
    1. public class Hero : Character {
    2.     public void Walk()
    3.     {
    4.             anim.SetTrigger(WALKANIMATION);
    5.             Vector3 destination = new Vector3(t.position.x + 1f, t.position.y, t.position.z);        
    6.             StartCoroutine(TranslateCorout(destination, movementspeed));
    7.     }
    8. }
    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 ^^
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When I saw your post, I was curious. I wasn't aware of this. I'm going to keep a watch on this thread to see if there's something I can learn :)
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, it works for me. I mean I have only a simply 'dummy' setup with a base class (holding the Enumerator) and a child class inheriting & calling it.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Should work. Try putting Debug.Log's in the coroutine, it'll fire. There's nothing special about inheritance here. Maybe movementSpeed is 0?
     
  5. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    yep. The issue is not on the inheritance.
    Neither on Coroutines.

    It's on the transform. position. It's the only spot in the code where I am modifying it's value. And if I print the value, I can see that it's correct. But it doesn't move. On the inspector its value never changed.

    Is it a known bug? Is there a way to lock a gameobject in a specific position and I did it accidentally?
     
  6. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    I mean...

    http://imgur.com/a/WTjRP

    This screen shows how I tried to move the hero with the translate coroutine as above (I'm printing the transform.position with a Debug.Log) from its initial position [-1,-1,0] to [0,-1,0]

    In the log, everything seems ok. But the transform hasn't changed.

    So, what am I changing? Why is not modificable? (I was moving that gameobject in this exact way until now..)
     
  7. Kwinten

    Kwinten

    Joined:
    Jan 25, 2015
    Posts:
    49
    The only explanation is that your Mecanim animation is overriding the position of the character. Have a look at those and see if you can find anything.
     
  8. Micio_del_Cheshire

    Micio_del_Cheshire

    Joined:
    Oct 24, 2013
    Posts:
    28
    Damn. This. So much this.

    It was on an animation that was never called, also. Totally resolved. Thanks a lot, to everyone ^^
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) Glad ya got it fixed.