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:

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.

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...
    move = true;
    //resets sound variables
    HasPlayed = false;
    soundTrigger = false;
    //If you are not looking at the object...
    //Make him look at the 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
                //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 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()
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;

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.