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 GroundGlobe · Aug 22, 2013 at 12:43 AM · movementaicharactercontrollermovement scriptmove

Moving AI away or towards player not working correctly.

What I am trying to do with the script below is to have the AI move towards the player once the player has entered the overlap sphere. But what happens instead is the AI moves away from the player. What is also weird is that if the player's X position is greater than zero the AI moves to the right only, if the X position is less than zero the AI moves left only. I've tried using Vector3.Lerp, it worked but I'd much rather use a character controller for movements. Anyways, here is the code.

 #pragma strict
 var health : float;
 var armor : float;
 var neutral = false;
 var vision_Range : int;
 var moveScript : AIMove;
 var rayLength : float;
 var mask : LayerMask;
 var targets : Transform = null;
 var controller : CharacterController;
 
 function Update(){
 var cols : Collider[] = Physics.OverlapSphere(transform.position, vision_Range, 1<<9);
                 //AI jumping
     if (Physics.Raycast(transform.position, Vector3.left, rayLength, mask)){
     moveScript.Jump();
     }else if (Physics.Raycast(transform.position, Vector3.right, rayLength, mask)){
     moveScript.Jump();
     }            //AI jumping
     
     for (var col : Collider in cols){
     targets = col.transform;
     }
     if (targets == null){
     controller.Move(Vector3.zero);
     }else{
     controller.Move(targets.position * Time.deltaTime * 4);
     }
 }
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 robertbu · Aug 22, 2013 at 12:51 AM

You have a number of issue here. Let me address the one in your question first. CharacterController.Move() takes a direction not a position in 3D space. To fix your movement problem replace line 27 with

 var moveDir = target.position - transform.position;
 controller.Move(moveDir.normalized * Time.deltaTime * speed);

Next is the 'if (tergets == null)'. Unless your intent is to have your AI return to the origin if it does not find the target, remove lines 24 - 26 and line 28. There is no reason to any action if your intent is for the AI to remain in place if a target is not found.

Lines 21 - 23 just cycle through the colliders of any targets and picks the last one in the list (if any). The list is not ordered, so I'm not sure why that code is there. In addition, if something is found in a previous frame and 'targets' get sets, and then there is nothing in the current frame, you code would execute on bad data. For now you could do something like:

 if (cols == null || cols.Length == 0)
     targets = null;
 else
     targets = cols[0].transform;


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

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

Related Questions

move enemy to old position of player 2 Answers

How do you move a character using a slider? 1 Answer

AI code for enemy car to follow the player car 0 Answers

Using Input.GetAxis on a 2 Player Game 2 Answers

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 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