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 Joetjah · Jan 24, 2013 at 10:28 PM · javascripttransformaicharactercontroller

AI movement using Character Controller, turning problem

I'm creating a Pacman game. The movement of the enemy AI is random for now. I've created code that would determine if obstacles are in front, left or right from the enemy using Raycasting. If the enemy can, he'll randomly walk in one direction that's available (not counting backwards). When the unit decides to go left/right, I try to turn the character since the Raycasting is done via Vector3.Right or Vector3.Left. But somehow, the unit doesn't seem to turn.

Here is some code:

 function MoveDirection(dirWay) {
     var direction : Vector3;
     if (Time.time > nextUpdate) {
         if (dirWay == 0)
             direction = transform.position + Vector3.forward * 2;
         if (dirWay == 1)
             direction = transform.position + Vector3.left * 2;
         if (dirWay == 2)
             direction = transform.position + Vector3.right * 2;
         nextUpdate = Time.time + (Random.value * howLong);
         //direction = Random.onUnitSphere;
         direction.y = 0;
         direction.Normalize ();
         direction *= howFast;
         direction.y = 1.5 - transform.position.y;
     }
     var controller = GetComponent(CharacterController);
     controller.transform.LookAt(direction*10);
     controller.Move(direction * Time.deltaTime);    
 }

What am I doing wrong?

EDIT: Two of the four ghosts walk in a corner and get stuck there, while the other two take three turns and get stuck in a corner there. They always perform the exact same behaviour every run even though it should be random.

Comment
Add comment · Show 1
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
avatar image cdrandin · Jan 24, 2013 at 10:58 PM 0
Share

Well Pacman has strict movements. $$anonymous$$eaning that in practice the ghost should be rotating in increments of 90 degrees or else they will continue to collide with the walls and never move forward properly. Try to only rotate 90 degrees depending where it needs to go, which will allow the ghost to move as straight as possible and stop getting stuck.

Also, it is not wise to keep calling GetComponent with the moveDirection function. Create private var controller : CharacterController then in the Start () type controller = GetComponent(CharacterController) which caches the info which will definitely boost the performance.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jan 24, 2013 at 11:49 PM

There are several errors in the logic:
- direction must be declared outside any function in order to hold its value until nextUpdate;
- don't add transform.position to direction: adding it will generate a point in the direction to move, what's needed only in the LookAt argument;
- there's some weird code after the last if that's screwing up direction.

 // declare direction outside Update, so that it will keep the last direction:
 private var direction = Vector3.zero;

 function MoveDirection(dirWay) {
     if (Time.time > nextUpdate) {
        if (dirWay == 0)
          direction = Vector3.forward;
        if (dirWay == 1)
          direction = Vector3.left;
        if (dirWay == 2)
          direction = Vector3.right;
        nextUpdate = Time.time + (Random.value * howLong);
        direction *= howFast;
     }
     var controller = GetComponent(CharacterController);
     // LookAt need a point, so you add transform.position here:
     controller.transform.LookAt(transform.position+direction);
     controller.Move(direction * Time.deltaTime);
 }
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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keeping Character From Walking Through Walls 1 Answer

Small Project, tutorials/help needed 0 Answers

Gravity with Character Controller? 4 Answers

i can't set destination, using AI Navmesh via raycast 1 Answer

Can't do spawning 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