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
1
Question by DarkZozoHD · Mar 04, 2012 at 03:39 PM · aimonster

Enemy AI monster problem

hi, I dont speak english really well but i can understand. I have a javascript code to make follow an object to another:

 var leader : Transform;

var follower : Transform;

var speed : float =5; // The speed of the follower

function Update(){

 follower.LookAt(leader);

follower.Translate(speed*Vector3.forward*Time.deltaTime);

animation.Play("Walk");

}

I use it for make an enemy but there is 2 problems : My enemy walk trough walls and can follow me from any distance, i want to controll it, please, can you help me ? [sry bad english] Bye :)

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
1

Answer by Peter G · Mar 04, 2012 at 03:58 PM

You don't want to use Transform.Translate() because that overrides the collision engine. It doesn't take much work to fix fortunately. Just add a character controller to your enemy. Then your code doesn't need many changes.

 var leader : Transform;
 var follower : Transform;
 var followerCC : CharacterController;
 
 var speed : float =5; // The speed of the follower
 
 function Start () {
      followerCC = follower.GetComponent.<CharacterController>();
 }
 
 function Update(){
 
     follower.LookAt(leader);
     followerCC.SimpleMove( follower.forward * speed );
     //See character controller.SimpleMove().  Moves the object in that direction.
     //automatically multiplies by Time.deltaTime;
     //And responds to collisions
     //ignores vertical component of vector and uses built-in gravity instead.
 
     animation.Play("Walk");
 
 }

I assume your code is attached to the monster so you don't have to cache the follower transform, but it doesn't hurt. In fact it will probably speed your code up some.

And you will have to explain to me the latter part of your question. I take it as you don't want the enemy following you forever i.e. it has a zone that it stays in. In that case, just create a large trigger around the area the enemy is allowed in. When the player enters the trigger, attack, and when he/she leaves, stop.

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
1

Answer by aldonaletto · Mar 04, 2012 at 04:01 PM

Translate doesn't check collisions - that's why the enemy pass through things. Usually we add a CharacterController to the enemy (menu Component/Physics/CharacterController), and move it using Move or SimpleMove. You should also check the distance from the target (leader, in your case), and stop following it when it's greater than the detect range:

var leader : Transform; var follower : Transform; var detectRange = 30.0; var speed : float = 5; // The speed of the follower

function Update(){ var dist = Vector3.Distance(leader.position, follower.position); if (dist

var leader : Transform; var follower : Transform; var detectRange = 30.0; var speed : float = 5; // The speed of the follower private var character: CharacterController;

function Update(){ // save the CharacterController in a variable to improve performance: if (!character) character = GetComponent(CharacterController); var dir = leader.position - follower.position; // find the leader direction dir.y = 0; // keep the direction in the horizontal plane if (dir.magnitude

Comment
Add comment · Show 2 · 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 DarkZozoHD · Mar 04, 2012 at 06:35 PM 0
Share

@aldonaletto There is error in your 2 scripts, it says : BCE0043 unexepted token: 30. could you fix it please, I need to choose the distance. thank you.

avatar image aldonaletto · Mar 04, 2012 at 07:35 PM 0
Share

That was an asha$$anonymous$$g syntax error! It should be:

 var detectRange : float = 30.0;

or simply

 var detectRange = 30.0;

I'm editing the answer, but UA is lasting an unpredictable amount of time to update things.

avatar image
0

Answer by DarkZozoHD · Mar 05, 2012 at 07:56 PM

Ok, thank you, it works Really well

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

6 People are following this question.

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

Related Questions

Bug with my enemyAI 2 Answers

Monster AI patrol an idle 0 Answers

Why does this script not work? 2 Answers

AI Airplane Problem 0 Answers

NavMesh AI: follow only if seen 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