- Home /
Movement Controller or movement function on every gameObject
Hello everyone,
So I'm trying to make a game with diferent unit types, each one with diferent movement speeds. I don't really know how to face this problem: At first I did a function on every unitType to define the speed but I don't think this is the most optimal way to do it. Now i was thinking about creating a MovementController class. This is what I did so far: First of all, I did a generic Unit Class -to keep it simple, I will just show the movement speed attribute- :
public class GenericUnit : MonoBehaviour
{
float movementSpeed;
public float mspeed
{
get { return movementSpeed; }
set { movementSpeed = value; }
}
}
after, I defined all atributes in a subClass depending of the unit type:
public class Archer : MonoBehaviour
{
GenericUnit archer;
void Start()
{
archer = new GenericUnit();
archer.mspeed = 3.0f;
}
}
This is the Movement controller I have right now:
public class Movement_Controller : MonoBehaviour
{
GenericUnit msUnit;
public float mspeed;
public bool movimentTrue;
void Start()
{
msunit = new GenericUnit();
mspeed = msUnit.mspeed;
movimentTrue = true;
}
void Update()
{
if (movimentTrue) {
Movement();
}
}
public void Movement(){
Vector3 moviment = new Vector3 (1.0f, 0.0f, 0.0f);
transform.position += Time.deltaTime * mspeed * moviment;
}
}
After all, this isn't working. Do you have any idea of whats the issue? I have been trying many other options that didn't make it. Do you think it's better to have this MovementController script or pasting the movement function on the Archer class? As you can see, im a novice on programming, so any suggestion would help a lot.
Thank you
Your answer
Follow this Question
Related Questions
Implementing Counter-Movement 0 Answers
Touch buttons for step movememt 3 Answers
Accelerometer 2 Answers
MouseLook character is acting wierd 1 Answer
HOW TO MAKE A NPC GO FORWAR 0 Answers