I’m building a game in the style of Super Monkey Ball. Both the enemy and the player use the same mesh etc and the only difference is color. Every actor should be driven by AddRelativeforces.
For my player, it’s easy - I capture Input.GetAxis and feed this to a move vector, then apply forces proportionally. No problem there.
But I’m at a loss on how to move my CPU-controlled enemies. If i manually apply forces, the move vector does not smoothly increase in magnitude over time like it does with my physical player: this is because Input.GetAxis returns a -1…1 value proportional to the ‘strength’ of the joystick move (this is simulated by unity when using a keyboard).
I was thinking Lerping to a certain value but I’m not sure how to do it… any help would be appreciated.
The correct Object Oriented design in my opinion, would be to have the “Actor” class, that will be used for both player and enemies. This actor class will be attached to the mesh object, and will contain an API to control this actor. In your example, it should have a method called “AddRelativeForces(Vector2 force)”, that will apply the relevant forces on the actor’s rigidbody:
public class Actor : MonoBehaviour {
protected void AddRelativeForces(Vector2 force) {
GetComponent<RigidBody2D>().AddForce(force);
}
}
Then, you will have two different controllers. One for the player, and one for the enemy. They will both make use of the actor’s API, but each will make it’s own decision of when to apply the force. The player will make the decision according to the keyboard input, and the enemy will make the decision according to your AI algorithm.
public class PlayerControl : MonoBehaviour {
public Actor actor;
void Update() {
Vector2 force = new Vector2(Input.GetAxis("playerX"), 0); // set force depending on controls
force = force * Time.deltaTime; // multiply by deltaTime to keep force independent of framerate
actor.AddRelativeForces(force);
}
}
And the enemy:
public class EnemyControl : MonoBehaviour {
public Actor actor;
void Update() {
Vector2 force = new Vector2(GetAiForceDirection(), 0); // set force depending on AI decision
force = force * Time.deltaTime; // multiply by deltaTime to keep force independent of framerate
actor.AddRelativeForces(force);
}
float GetAiForceDirection() {
// Implement your AI algorithm here
}
}