I wrote an extension method to make the object move over time. This works well; however, since the object performs this task for a certain period of time, it ignores all other calls, such as my update method. I guess I need to do something with Coroutine, but I can't figure out where this is going. How can I do the following code work without preventing the syntax of another code from starting (for example, the Update () method)? A simplified version is as follows:
==================================================== ==================================
[RequireComponent(typeof(Rigidbody2D)]
public class MyBehaviour : MonoBehaviour
{
void Start()
{
rigidbody2D.MoveOverTime();
}
void Update(){
rigidbody2D.MovePosition(transform.position.x + 1, transform.position.y);
}
}
==================================================== ==================================
public static class Rigidbody2DExtension
{
public static void MoveOverTime(this Rigidbody2D rigidbody2D)
{
gameObject.addComponent<MoveOverTimeComponent>();
}
}
[RequireComponent(typeof(Rigidbody2D)]
class MoveOverTimeComponent : MonoBehaviour
{
void Update(){
MovePositionByALittleBit();
}
void MovePositionByALittleBit(){
float x = transform.position.x + 1;
float y = transform.position.y;
rigidbody2D.MovePosition(new Vector2(x, y));
}
}
source
share