Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by JUnityer · Jul 04, 2011 at 08:40 AM · movementaicharactercontrollerpatrol

Make AI move using CharacterController.Move instead of rigidbody.velocity?

Hi all!

I am having a big trouble customizing my patrolling AI script to fit into a Pac-Man style stealth game. My current script has parts of a tutorial and parts I wrote myself.

I think it would move better if I used CharacterController.Move, but tell me if there is a better way!

I already tried taking parts of that move script I linked, but somehow I couldn't get it working properly...

So if you please could point me out what to do, I'm so confused with this :S Here is the script:

 var waypoint : Transform[];
 var speed : float = 50;
 private var currentWaypoint : int;
 var loop : boolean = true;

 var player : Transform;

 function Update () 
 {
     if(currentWaypoint < waypoint.length)
     {
         var target : Vector3 = waypoint[currentWaypoint].position;
         var moveDirection : Vector3 = target - transform.position;
         var velocity = rigidbody.velocity;
             if(moveDirection.magnitude < 1)
             {
                 currentWaypoint++;
             }
             else
             {
                 velocity = moveDirection.normalized * speed;
             }
     }
     
     else
     {
         if(loop)
         {
             currentWaypoint=0;
         }
         else
         {
             velocity = Vector3.zero;
         }
     }
 
         rigidbody.velocity = velocity;
         transform.LookAt(target);
     
 }

 function LateUpdate()
 {
          //This prevents the enemy from flying upwards
          transform.position.y = 1.7;
 }


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

3 Replies

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

Answer by aldonaletto · Jul 04, 2011 at 01:54 PM

You're right: the best way to control characters is... the CharacterController! Moving rigidbodies always have collateral effects. I converted your script to use CharacterControllers instead of rigidbodies. Create or import your character, add the CharacterController using the menu Components->Physics->Character Controller (it will replace the original collider, if any), then add this script to the character.

 var waypoint : Transform[];
 var speed : float = 50;
 private var currentWaypoint : int = 0;
 var loop : boolean = true;
 var player : Transform;
 private var character : CharacterController;
 
 function Start ()
 {
     character = GetComponent(CharacterController);
 }
 
 function Update () 
 {
     if(currentWaypoint < waypoint.length)
     {
         var target : Vector3 = waypoint[currentWaypoint].position;
         target.y = transform.position.y; // keep waypoint at character's height
         var moveDirection : Vector3 = target - transform.position;
         if(moveDirection.magnitude < 1)
         {
             transform.position = target; // force character to waypoint position
             currentWaypoint++;
         }
         else
         {
             transform.LookAt(target);
             character.Move(moveDirection.normalized * speed * Time.deltaTime);
         }
     }
     else
     {
         if(loop)
         {
             currentWaypoint=0;
         }
     }
 }
Comment
Add comment · Show 5 · 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
avatar image JUnityer · Jul 04, 2011 at 02:07 PM 0
Share

Thank you sooo much! I have been trying to figure this out for like a week now, but this seemed to be pretty simple. I'll learn from this. Thanks again! :)

avatar image kevinseligmann · Aug 23, 2012 at 06:40 PM 0
Share

Hi! Your script is very very useful, but I have a question regarding how it works with the CharacterController. By using this script to make a 'wander' effect on a AI Player, how can I manage to move the AI Character AND play an animation? $$anonymous$$y AI Character has a 'run' animation and the CharacterController script. Can these two be combined? Thanks!

avatar image kevinseligmann · Aug 23, 2012 at 06:44 PM 0
Share

Sorry, I forgot to mention: For the example I just gave you, I'm using the Third Person Controller that cames in the standar assets with the builted-in animations. I just remove the camera script.

avatar image aldonaletto · Aug 24, 2012 at 09:33 PM 2
Share

You can change this same script to check the character speed and select the animation according to its actual velocity - like this:

... private var character : CharacterController; private var lastPos : Vector3; // declare this variable

function Start () { character = GetComponent(CharacterController); lastPos = transform.position; // initialize it }

function Update () { // calculate displacement since last frame: var delta = transform.position - lastPos; lastPos = transform.position; // update last position delta.y = 0; // consider only the horizontal displacement // calculate the actual horizontal velocity: var vel = delta.magnitude / Time.deltaTime; if (vel < 0.2) animation.CrossFade("idle"); // lower than 0.2: idle else if (vel < 3.0) animation.CrossFade("walk"); // between 0.2 and 3.0: walk else animation.CrossFade("run"); // higher than 3.0: run // continue original code: if(currentWaypoint < waypoint.length) { ...

avatar image koboldskeep · Sep 02, 2013 at 05:31 AM 0
Share

Thanks! That helped a lot, I couldn't figure out how to extend the CharacterController properly.

avatar image
0

Answer by John-S206 · Jun 25, 2016 at 07:44 PM

I've been wondering how i could make a GameObject follow anything.

I've found a solution, well it took along time but i would like to share it with you.

Here is the link https://jsoshsn.makes.org/thimble/LTE4MDAxNDIzMzY=/ai-fcs

Copy this into a JavaScript and watch.

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
avatar image
0

Answer by atifatgamesentity · Nov 24, 2020 at 10:05 PM

Here is a new updated Script ... link text


charactermovementnew.zip (1.2 kB)
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

9 People are following this question.

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

Related Questions

Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer

Keeping Character From Walking Through Walls 1 Answer

Small Project, tutorials/help needed 0 Answers

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

How to forbid an NPC to move diagonally using Character Controller? 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