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 /
This question was closed Jun 17, 2013 at 05:46 PM by Fattie for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by isfrseirra259 · Jun 17, 2013 at 08:22 AM · javascriptaifollow player

How to create an AI follow script in Java?

Hey guys, I've been trying to create a enemy AI follow script in Java in which the enemy object will always face and follow the first-person player at a given speed.

I've tried to use this script as an example, but all I end up with is the enemy spinning around in a circle:

http://answers.unity3d.com/questions/26177/how-to-create-a-basic-follow-ai.html

The overall idea is to that I am going to create a "Weeping Angel" style enemy character in which it will move closer to the player whenever the player is not looking at it, but will freeze as soon as the player looks at the "Angel". So far, I've encountered problems like the Angel doesn't move at all or that it warps to funny positions and rotations, and clips into the terrain and other objects.

So what kind of code would I have to write to fix these problems? Help much appreciated.

P.S. Code must be in Javascript.

Comment
Add comment · Show 8
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 Xazas · Jun 17, 2013 at 08:47 AM 0
Share

Have you tagged your player correctly as "Player"? This script wont prevent you "Angel" from clipping into the terrain or other obstacles, for that you have to use a character controller.

avatar image isfrseirra259 · Jun 17, 2013 at 09:28 AM 0
Share

Yes, I've tagged the object correctly and whatnot. Still only goes in circles for some reason....

avatar image Xazas · Jun 17, 2013 at 09:40 AM 0
Share

I had a similar problem with such a code when I started with Unity a while ago. Have you imported a model for the angel from somewhere(maybe Blender)? $$anonymous$$ake sure the local axis of your model are correct(y is up, z is forward). It can happen that they are reversed when exporting from a modeling software.

The Problem than is the Quaternion.LookRotation wich takes the y axis as up by default.

avatar image isfrseirra259 · Jun 17, 2013 at 09:43 AM 0
Share

Yep, I realised that problem and made the Z as forward and Z as up. I made the model in Blender, set the correct axis and export/imported it as a 3ds file.

avatar image superventure · Jun 17, 2013 at 09:44 AM 0
Share

Try experimenting with just transform.Lookat and see if there is any difference.

Show more comments

2 Replies

  • Sort: 
avatar image
0

Answer by isfrseirra259 · Jun 17, 2013 at 12:54 PM

Alrighty, here's my final code: (I took some parts of a previous Angel Script I found floating around and applied a few adjustments. The Weeping Angel now follows the player whenever the it is not being Rendered. It will freeze whenever it IS rendered. Also, I've added a few additional "jumpscare" features such as if the player looks at it, a sound will play. No need for tagging stuff. Make sure both target and pos are the Player's character.

 //target to follow
 var target : Transform;
      
 //Checks his position, used to follow the target
 var pos : Transform;
      
 //Ray variables (Length... etc.)
 var rayLength : float;
      
 //Movement, speed etc.
 var speed : float;
      
 //You can move if he is not being looked at
 var move : boolean = false;
 
 //Sets the sound effect trigger to be false at start
 var soundTrigger : boolean = false;
 
 //Defines your sound effect
 public var Sound : AudioClip;
 
 //Defines whether the sound has been played before.
 private var HasPlayed : boolean = false;
 
      
 //I had problems with my model sinking into the floor, adjust this variable if you need it, or remove it
 /*function FixedUpdate()
 {
 transform.position.y = 30;
 transform.rotation.x = 0;
 transform.rotation.z = 0;
 }*/
      
 function Update()
 {
     //Setting up Raycast variables for simple object avoidance
     var fwd = transform.TransformDirection (Vector3.forward);
     var hit : RaycastHit;
     
     //If you are looking at the object...
     if (renderer.isVisible)
     {
     move = false;
     soundTrigger = true;
     }
        
     //If you are NOT looking at the object...
     if(!renderer.isVisible)
     {
     move = true;
     //resets sound variables
     HasPlayed = false;
     soundTrigger = false;
     }
      
     //If you are not looking at the object...
     if(move)
     {
     //Make him look at the target
     transform.LookAt(target);
     //Always follow the target
     transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
             //Sets the sound effect componnet. If the player is looking, the sound will play.
             if (soundTrigger)
             {
                 //Make sure the sound effect has not been played before. (Without this, this tends to lead to a repeated playing of the sound effect with every frame
                 if(!HasPlayed)
                 {
                 audio.PlayOneShot(Sound);
                 //Sets boolean to define that the above sound has been play already, and does not need to be played again while the player is looking at the target.
                 HasPlayed = true;
                 }
             }
     //If he is 3 units away from something, move right (Works if you are not looking at the object)
     if (Physics.Raycast (transform.position, fwd, rayLength) && move)
     {
     Debug.Log("Something ahead, moving");
     transform.Translate(Vector3.right * 3 * Time.deltaTime);
     }
 }
Comment
Add comment · Show 1 · 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 DricoJD · Jun 17, 2013 at 01:46 PM 0
Share

This answer will work as well guys, the one below (my one) is a little bit more flexible and will run faster, the one above is more advance and probably will not be as good for the little developer to use as it will be a struggle to understand.

But choosing a script is up to you.

Good answer anyway isfeseirra259

avatar image
0

Answer by DricoJD · Jun 17, 2013 at 01:43 PM

This script below should be used as the initial setup:

 var walkSpeed = 3.0;
 var rotateSpeed = 30.0;
 var attackSpeed = 9.0;
 var attackRotateSpeed = 100.0;
 var directionTraveltime = 2.0;
 var idletime = 1.5;
 var attackDistance = 10.0;
 var damage = 2;
 var viewAngle = 20.0;
 var attackRadius = 1.0;
 var attackPosition = new Vector3 (0,1,0);
 
 private var isAttacking = false;
 private var lastAttackTime = 0.0;
 private var nextPauseTime = 0.0;
 private var distanceToPlayer;
 private var timeToNewDirection = 0.0;
 
 var target : Transform;

 
 private var characterController : CharacterController;
 characterController = GetComponen(Charactercontroller);


Now use this:

 function start()
 {
 if(!target) 
 target = GameObject.FindWithtag("Player").transform;
 }
 
 //add all your animation stuff here or take animation variables away.
 
 
 function Idle()
 {
 var walkForward = transform.TransformDirection(Vector3.forward);
 characterController.SimpleMove(walkForward * walkSpeed);
 distanceToPlayer = transform.position - target.position;
 //when you find the player
 if (distanceToPlayer.magnitude <attackdistcance) return;
 yield;
 }


Some of the variables will not be needed if so just delete them

I have just writting this all out new, so please take the time to thumb this up, many thanks.

James

Comment
Add comment · Show 1 · 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 maxjadin · Feb 26, 2014 at 11:36 PM 0
Share

thank you it works

Follow this Question

Answers Answers and Comments

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

Related Questions

Enemy Attack Animation 1 Answer

How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers

AI Path and respawn help!!! 0 Answers

2D sidescroller ranged character shooting problem. 0 Answers

What should I do? Disable the AI or have the AI simply be recylced and reused? 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