Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
0
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
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

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


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