- Home /
Problem is not reproducible or outdated
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.
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.
Yes, I've tagged the object correctly and whatnot. Still only goes in circles for some reason....
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.
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.
Try experimenting with just transform.Lookat and see if there is any difference.
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);
}
}
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
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
Follow this Question
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