• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by joeyrubio · Mar 08, 2011 at 12:46 AM · movementaicharactercontrollernpcdiagonal

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! :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

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 &gt; approximateDistance)
     var theTarget = new Vector3(1,0,0);
 else if (toTarget.z &gt; 0.2) 
     theTarget = new Vector3(0,0,1);
 else {
     if (toTarget.x &lt;= -approximateDistance)
         theTarget = new Vector3(-1, 0, 0);
     else if (toTarget.z &lt;= -approximateDistance)
         theTarget = new Vector3(0, 0, -1);
 }
 controller.Move(theTarget * Time.deltaTime * speed);

 if (toTarget.magnitude != 0)
     transform.rotation = Quaternion.LookRotation(toTarget);

}

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to check if a character hasn't changed its position in the last x seconds? 1 Answer

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

SendMessage/BroadcastMessage has no receiver EnemyAI to NPCMovement 2 Answers

Moving AI away or towards player not working correctly. 1 Answer

Player moves diagonally even when not telling him to 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges