I creating an AI for my enemy. My enemy is a cylinder. I want it to start standing up, and when the player is within activateRange, I scripted it to fall over on its side using a random AddForce() and set activated to true. This works fine, its just that the rest of the script is super derpy. Once activated, it should turn to, and follow the player when it is within chaseRange. I want the cylinder to roll on its side towards the player. But right now, it is violently shaking, not always going towards me, and not rolling on its side like I want it to. I hope I’m being clear, if not feel free to ask me to clarify.
Thanks!
#pragma strict
public var myTransform : Transform; //current transform data of this enemy
public var target : Transform; // The transform of the player
public var moveSpeed = 5.0; // the rate at which the enemy follows the player
public var rotationSpeed : float = 10.0; // the rate at which the enemy turns towards the player
public var activateRange : float = 25.0; // range at which enemy is activated
public var chaseRange : float = 55.0; // range at which the enemy follows the player once activated
public var dieRange : float = 1.0; // range at which the enemy blows up at the player. This is not really relevent for the question, sorry
public var activated : boolean = false; // if the enemy is activated or not
var once : boolean = true; // makes sure that the force is not added multiple times, because this function is called every Update() frame
function Start()
{
}
function Update()
{
if (target == null){ // if there is no player
return;
}
var range: float = Vector3.Distance(target.position, transform.position); // calculates the distance between the player and the enemy
if(range <= activateRange){ // if the player is within activation range
Activate(); // then activate. see the Activate() function before continuing
}
if(activated){ // makes sure the enemy is activated
if (range <= dieRange){ // tests to see if the ennemy is within the range to attack the enemy
//Boom - still yet to do
}
else if (range <= chaseRange){ // if player is within the chase range, then chase
//this is the derpy part
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
function Activate(){
var random = Random.Range(1,9); // picks a random number between 1 and 8
var xpos : float; // random x value for AddForce
var zpos : float; // random z value for AddForce
if(random == 1){ // if random == 1, choose random AddForce1
xpos = 0.4;
zpos = 0.4;
} else if(random == 2){ // ect.
xpos = -0.4;
zpos = -0.4;
}else if(random == 3){
xpos = -0.4;
zpos = 0.4;
}else if(random == 4){
xpos = 0.4;
zpos = -0.4;
}else if(random == 5){
xpos = 0.6;
zpos = 0;
}else if(random == 6){
xpos = -0.6;
zpos = 0;
}else if(random == 7){
xpos = 0;
zpos = 0.6;
}else if(random == 8){
xpos = 0;
zpos = -0.6;
}
if(once){ // again makes sure it is onle added once
once = false;
rigidbody.AddForce(xpos, 1, zpos * 500); // knocks over enemy onto side
yield WaitForSeconds (5); // waits until enemy is on ground before running the follow part of the script
activated = true; // activates the enemy
}
}