- Home /
Design pattern of custom animation class
Hello. I am building some custom class for my game object to play animation like moving on a straight line or scaling. As the parameters always change, I build new class to control the animation rather than using animator or animation provided by Unity (maybe animator can also handle it?).
For the game object which needs an animation, just simply adding the component to it and call start function. I show an example here:
public class generateVideo : MonoBehaviour {
void Start()
{
gameObject.AddComponent<animeMoving>();
GetComponent<animeMoving>().startMoving(0.2f, new Vector3(1, 10, 1));
}
}
And here is the class animeMoving:
public class animeMoving : MonoBehaviour
{
float speed = 0; // cannot be negative
Vector3 endPos = new Vector3(0, 0, 0);
void Update() {
if (speed > 0) {
transform.position = Vector3.MoveTowards(transform.position, endPos, speed);
if (transform.position == endPos) {
speed = 0;
}
}
}
public void startMoving(float movingSpeed, Vector3 endPosition) {
endPos = endPosition;
speed = movingSpeed;
}
public void stopMovingNow() {
speed = 0;
}
}
From now on, these classes are stored at a .cs file. Is there any design pattern suggested to use in order to make it more readable?
Your answer

Follow this Question
Related Questions
2D Animation does not start 1 Answer
Simple question about OnMouseButtonAsUp 1 Answer
Vehicle Enter Script Errors HELP! 0 Answers
Disable components in runtime per quality setting ? 1 Answer
How do I disable Script on FP Controller? (Solved) 2 Answers