- Home /
How to forbid an NPC to move diagonally using Character Controller?
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! :)
Answer by joeyrubio · Mar 08, 2011 at 08:09 PM
Well.. I managed to find a solution..
var approximateDistance : float = 0.1;
 
               function move(aTarget : Transform) : void { var speed : float; 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 <= approximateDistance*2) { reactToState(state); return; }
  if (toTarget.x > approximateDistance)
     var theTarget = new Vector3(1,0,0);
 else if (toTarget.z > 0.2) 
     theTarget = new Vector3(0,0,1);
 else {
     if (toTarget.x <= -approximateDistance)
         theTarget = new Vector3(-1, 0, 0);
     else if (toTarget.z <= -approximateDistance)
         theTarget = new Vector3(0, 0, -1);
 }
 controller.Move(theTarget * Time.deltaTime * speed);
 if (toTarget.magnitude != 0)
     transform.rotation = Quaternion.LookRotation(toTarget);
 }  
Your answer
 
 
             Follow this Question
Related Questions
How to check if a character hasn't changed its position in the last x seconds? 1 Answer
Moving AI away or towards player not working correctly. 1 Answer
SendMessage/BroadcastMessage has no receiver EnemyAI to NPCMovement 2 Answers
Make AI move using CharacterController.Move instead of rigidbody.velocity? 3 Answers
Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                