My game doesn't allow human players to move diagonally when played by 2 players. I started implementing the AI for when its only one player playing and so I need the computer to control the other player. Everything works perfectly fine except that it moves diagonally, which I dont want it to, and even though I have tried many things, they haven't worked. Any ideas?
This is how human player characters are forbidden to move diagonally:
// Simple character controller movement with the keyboard.
function keyboardMove() {
var toTarget : Vector3 = new Vector3();
if (p1) {
if (Input.GetAxisRaw("P1Vertical")) {
toTarget.z = Input.GetAxis("P1Vertical");
controller.Move(toTarget * Time.deltaTime * speed);
}
else if (Input.GetAxisRaw("P1Horizontal")) {
toTarget.x = Input.GetAxis("P1Horizontal");
controller.Move(toTarget * Time.deltaTime * speed);
}
}
else if (p2) {
if (Input.GetAxisRaw("P2Vertical")) {
toTarget.z = Input.GetAxis("P2Vertical");
controller.Move(toTarget * Time.deltaTime * speed);
}
else if (Input.GetAxisRaw("P2Horizontal")) {
toTarget.x = Input.GetAxis("P2Horizontal");
controller.Move(toTarget * Time.deltaTime * speed);
}
}
if (toTarget.magnitude != 0) {
transform.rotation = Quaternion.LookRotation(toTarget);
}
}
And this is how the NPC keeps moving the way it wants...
function move(aTarget : Transform) {
var toTarget : Vector3 = new Vector3();
toTarget.x = aTarget.transform.position.x - transform.position.x;
toTarget.y = 0;
toTarget.z = aTarget.transform.position.z - transform.position.z;
if(toTarget.magnitude <= 0.2) {
reactToState(state);
return;
}
toTarget.Normalize();
controller.Move(toTarget * Time.deltaTime * speed);
if (toTarget.magnitude != 0) {
transform.rotation = Quaternion.LookRotation(toTarget);
}
}
Even if I don't find the answers I'm pretty sure these are 2 useful (part of) scripts anyone can use! :)